在 UIButton 中实现切换功能
我想知道如何向 UIButton
添加切换功能,例如用户点击未选择的按钮,该按钮将被选中并保持选中状态,直到用户再次点击它,就像以前一样取消选择。
我正在考虑制作一个 IBAction
,将其从未选中更改为选中,我该怎么做?
这是我尝试过的:
-(IBAction)toggle {
//Toggle on implementation.
button.selected = YES;
button.highlighted = NO;
button.enabled = YES;
//Toggle off implementation.
if (button.highlighted == YES) {
button.selected = NO;
button.highlighted = YES;
button.enabled = NO;
}
}
问题...
-(IBAction)toggleFav {
if (favButton == nil) {
UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
}
if([favButton isSelected]){
//Add to menu.
[favButton setSelected:NO];
} else {
//Remove from menu.
[favButton setSelected:YES];
}
}
I want to know how to add the toggling feature to a UIButton
, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.
I was thinking of making an IBAction
which changes it from unselected to selected, how can I do that?
Heres what I tried:
-(IBAction)toggle {
//Toggle on implementation.
button.selected = YES;
button.highlighted = NO;
button.enabled = YES;
//Toggle off implementation.
if (button.highlighted == YES) {
button.selected = NO;
button.highlighted = YES;
button.enabled = NO;
}
}
Problem...
-(IBAction)toggleFav {
if (favButton == nil) {
UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
}
if([favButton isSelected]){
//Add to menu.
[favButton setSelected:NO];
} else {
//Remove from menu.
[favButton setSelected:YES];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
toggleFav
代码没有多大意义。if (favButton == nil) {
检查favButton
是否存在。但如果你用 IB 连接它,它应该总是在那一点上。如果不是的话,按钮怎么会调用这个方法呢?所以这样做:在这里你会找到一个示例项目,带有一个 DetaiView,其中包含一个具有 2 个状态的 Button。
注意:我正在 NSUserDefaults 中保存所选按钮的信息。你不应该那样做。相反,您需要将其保存在模型中。但由于我没有关于你的模型的信息,我只是使用 NSUserDefaults。
Your
toggleFav
code doesn't make much sense.if (favButton == nil) {
checks, iffavButton
is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:Here you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.
使用button.highlighted属性
Use button.highlighted property
你应该这样编码:
You should code like this: