我正在尝试通过带有标题和图像的 UIButton
为我的 iPhone 应用程序创建一个标准复选框。按钮图像在“未选中”图像和“已选中”图像之间变化。
起初我尝试子类化 UIButton
但是 UIButton
没有 -init***
方法可以用在我的 -init
方法。
执行此操作的最佳方法是什么?
提前致谢。
最佳答案
您不需要子类化 UIButton
类。在设计上,Objective-C 更倾向于组合而不是继承。
UIButton
是 UIControl
的子类,它有一个 selected
属性。您可以使用此属性来切换复选框的开/关行为,就像 UISwitch
所做的一样。
你可以在按钮的 touched up 内部事件上附加一个 Action ,并在那里执行切换,就像这样:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
关于iphone - 制作 UIButton 复选框的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2227366/