ios - UIButton - 复选标记未显示

标签 ios objective-c uibutton

我正在尝试创建一个 uibutton 作为复选框。一切正常,但 View :我正在设置单击时更改 View 。当它应该不显示复选标记时很好,但是当我需要显示复选标记时,它不起作用。这就是发生的事情:

image with no checkmark
image with checkmark

这是代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.myButton setTitle:@"\u2610" forState:UIControlStateNormal];
    [self.myButton setTitle:@"\u2610" forState:UIControlStateHighlighted];
    [self.myButton setTitle:@"\u2610" forState:UIControlStateDisabled];
    [self.myButton setTitle:@"\u2610" forState:UIControlStateNormal];
    [self.myButton setTitle:@"\u2611" forState:UIControlStateSelected];
}

//action sent to the button when touched up inside
- (IBAction)moneyButtonClicked:(id)sender {
    UIButton *button  = (UIButton *)sender;
    button.selected = !button.selected;
}

怎么了?

问候!

最佳答案

我只是创建一个新项目并添加以下代码。它就像 gif 显示的那样工作。你可以做一个比较。

enter image description here

- (void)viewDidLoad {
    [super viewDidLoad];
    self.checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.checkButton addTarget:self action:@selector(moneyButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.checkButton.frame = CGRectMake(50, 50, 50, 50);
    self.checkButton.titleLabel.textColor = [UIColor redColor];
    self.checkButton.backgroundColor = [UIColor yellowColor];
    [self.checkButton setTitle:@"\u2610" forState:UIControlStateNormal];
    [self.checkButton setTitle:@"\u2611" forState:UIControlStateSelected];
    [self.view addSubview:self.checkButton];
}

- (IBAction)moneyButtonClicked:(id)sender {
    UIButton *button  = (UIButton *)sender;
    button.selected = !button.selected;
}

关于ios - UIButton - 复选标记未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925799/

相关文章:

ios - 为什么在我的应用程序中使用 numberOfRowsInSection 导致永无止境的循环?

ios - 具有 Web View 的模态视图使应用程序在 iOS7 中崩溃

ios - 如何使用 Swift 3 为按钮制作摇动动画

iphone - 添加自定义 UIButton 到 subview

ios - 根据 UIButton 的标题调整其宽度

javascript - iOS 上 jquery ui datepicker 奇怪的触摸/点击行为

ios - MapKit 中 MKMarkerAnnotationView 的彩色图像

ios - 无法在 TableView Controller 中为单元格设置自定义类

objective-c - 如何读取和解析 .xml 文件

ios - 在已创建的对象 "the name of the class i' 上找不到属性”,并且 View Controller 无法识别其中定义的方法