Eli*_*hme 1 objective-c uibutton ios
由于UISegmentedControl无法在IOS 7中自定义,并且由于我的项目需要一定程度的自定义,因此我决定手动创建两个UIButton,使用bool变量来指示单击哪个按钮,然后重新加载tableView:
- (IBAction)pastFun:(id)sender {
statusClicked = FALSE;
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"PAST"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[pastBut setAttributedTitle:commentString forState:UIControlStateNormal];
[self.tableView reloadData];
}
- (IBAction)pendingFun:(id)sender {
statusClicked = TRUE;
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
在pastFun中,我添加了以下块,以便在选中时为该按钮加下划线,并且它可以工作:
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"PAST"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[pastBut setAttributedTitle:commentString forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
我想要做的是,当调用pendingFun(单击下一个按钮)时,从第一个按钮删除下划线,然后在第二个按钮下划线,我似乎无法找到正确的方法来完成它.有什么想法怎么做?
我认为最好选择并取消选择按钮.
- (void)viewDidLoad
{
[super viewDidLoad];
NSAttributedString *attrNormal = [[NSAttributedString alloc] initWithString:@"Button" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleNone]}];
NSAttributedString *attrSelected = [[NSAttributedString alloc] initWithString:@"Button" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleSingle]}];
UIButton* boton = [[UIButton alloc] initWithFrame:CGRectMake(20, 50, 100, 30)];
[boton setAttributedTitle:attrNormal forState:UIControlStateNormal];
[boton setAttributedTitle:attrSelected forState:UIControlStateSelected];
[boton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:boton];
}
- (void) buttonPressed:(UIButton *) boton {
boton.selected = !boton.selected;
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下,有两个按钮.您只需要保留一个属性来存储所选按钮(例如self.selectedButton).并在buttonPressed执行此操作:
- (void) buttonPressed:(UIButton *) boton {
self.selectedButton.selected = NO;
self.selectedButton = boton;
self.selectedButton.selected = YES;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2633 次 |
| 最近记录: |