正确更改按钮状态和属性。

Changing button states and properties correctly.

在我的应用程序中,我有 5 个按钮。当firstButton.selected = YES;我需要别人*.selected = NO;。其他四个按钮写"NO"是否正确?

如果不清楚我的意思,这里是示例代码:

- (IBAction)setColorRed:(id)sender {

            _redColor.selected = YES;
            _greenColor.selected = NO;
            _blueColor.selected = NO;
            _yeallowColor.selected = NO;
            _clearColor.selected = NO;
    }

2)问题的第二部分:

self.redColor setBounds:CGRectMake(0, 0, 45, 45)];

谁能用人类的语言解释一下为什么我改变了那些“0, 0”按钮并没有改变它的位置?就像发送 setFrame 时一样。

3)第三部分问题:

发件人是按钮。如果它是 UITableViewCell 的 属性,[sender isSelected] 怎么工作? "notSelected" 的等价物是什么?

提前致谢..

  1. 您应该将按钮放在数组中 - 然后您可以简单地跟踪所选索引并使用一个按钮按下方法并消除重复代码。

例如

- (IBAction)buttonTapped:(UButton *)sender {
    for (UIButton *aButton in self.buttons) {
        if (aButton != sender) {
            aButton.selected=NO;
        }
    }
    sender.selected=YES;
 }
  1. Bounds 是项目在其自身坐标 space 中的坐标,frame 是项目在其父坐标 space 中的坐标。尝试在它自己的 space.
  2. 中偏移项目的来源是没有意义的
  3. isSelected 的 'opposite' 只是检查 `isSelected' 的 false/NO 值 -

例如

if (self.redColor.isSelected) {
      //The button isn't selected
}

最后,你应该养成使用 self.property 而不是 _property 的习惯,除非你特别想绕过 setter/getter.

  1. 没关系,五个按钮一个人也不会太多。不过我不确定你想做什么。

  2. 位置不会改变,因为您正在为已有位置的对象设置边界。当您设置将设置位置的框架时,边界将基于您创建的框架。这是一个good explanation

  3. 发件人是用于调用该方法的任何内容,可以是按钮或单元格或您选择的任何内容。没有这样的事情 "not selected" 它只是 .selected = NO 而 "selected" 是 .selected = YES.

如果您需要更多说明,请告诉我。