iphone - 制作 UIButton 复选框的最佳方法是什么?

标签 iphone objective-c checkbox uibutton

我正在尝试通过带有标题和图像的 UIButton 为我的 iPhone 应用程序创建一个标准复选框。按钮图像在“未选中”图像和“已选中”图像之间变化。

起初我尝试子类化 UIButton 但是 UIButton 没有 -init*** 方法可以用在我的 -init 方法。

执行此操作的最佳方法是什么?

提前致谢。

最佳答案

您不需要子类化 UIButton 类。在设计上,Objective-C 更倾向于组合而不是继承。

UIButtonUIControl 的子类,它有一个 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/

相关文章:

iphone - 我可以在 iPhone 上缓存音频吗?

iphone - Flurry 应用程序的 session 数据需要多长时间才能显示在 Flurry 网站上

ios - 如何通过动画同时增加单元格高度和 subview 高度?

IOS宏问题

ios - 移除所有影响 UIView 的约束

iphone - RhoStudio 未内置于 XCode - Curl

iphone - 从横向开始的 Uiwebview 无法正确调整为纵向,但从纵向开始一切正常

css - IE/Edge 中复选框失败的切换布局

html - 复选框宽度/高度不会因较新的浏览器而改变

javascript - Angular JS 全选/取消全选复选框如何检索所选信息