0

我有一个没有标题或初始背景图像的 UIButton。按下时,按钮背景图像发生变化,再次按下时,它设置为 nil。现在在 iOS 6.x 中,该按钮完全消失了。这是我的代码:

- (IBAction)resetAll: (id) sender {
[self reset];

for (UIView *view in self.view.subviews) {

    if ([[[view class] description] isEqualToString:@"UIRoundedRectButton"]) {
        UIButton *theButton = (UIButton *)view;         

        int nTag = theButton.tag;

        switch (nTag) {
            case 11: {
                theButton.tag = 10;
                [theButton setBackgroundImage:nil forState:UIControlStateNormal];
                break;
            }
            case 21: {
                theButton.tag = 20;
                [theButton setBackgroundImage:nil forState:UIControlStateNormal];
                [theButton setEnabled:YES];
                break;
            }
            case 31: {
                theButton.tag = 30;
                [theButton setBackgroundImage:nil forState:UIControlStateNormal];
                [theButton setEnabled:YES];
                break;
            }
            case 41: {
                theButton.tag = 40;
                [theButton setBackgroundImage:nil forState:UIControlStateNormal];
                [theButton setEnabled:YES];
                break;
            }
            case 51: {
                theButton.tag = 50;
                [theButton setBackgroundImage:nil forState:UIControlStateNormal];
                [theButton setEnabled:YES];
                break;
            }
        }
    }
}
return;

此代码工作正常:

- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;

// Get the current tag value
int currentTag = [sender tag];

// Toggle the check buttons
if (currentTag == 10 || currentTag == 20 || currentTag == 30 || currentTag == 40 || currentTag == 50) {
        [btn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
        btn.tag = ++currentTag;
}

else
    if (currentTag == 11 || currentTag == 21 || currentTag == 31 || currentTag == 41 || currentTag == 51) {
        [btn setBackgroundImage:nil forState:UIControlStateNormal];
        btn.tag = --currentTag;
}

[self calculate];

任何建议或帮助将不胜感激!

4

3 回答 3

1

与其在 UIControlStateNormal 中更改按钮的背景图像,不如直接更改按钮的状态?如果您以编程方式创建按钮,则只需添加该行

[btn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateSelected];

然后,当按下并且您希望它成为箭头图像时:

[btn setSelected:YES];

并将其设置回默认外观:

[btn setSelected:NO];

如果您在 XIB 中制作了按钮,则可以通过将状态配置更改为 Selected 并在此处设置图像来设置状态图像。

于 2013-07-17T18:17:07.530 回答
0

这是实现 DOC 答案的最终代码。我在界面生成器中设置了默认和选定状态:

- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;

if ([btn isSelected ]) {
    [btn setSelected:NO];
} else {
    [btn setSelected:YES];
}

[self calculate];
}


- (IBAction)resetAll: (id) sender {
[self reset];

for (UIView *view in self.view.subviews) {

    if ([[[view class] description] isEqualToString:@"UIRoundedRectButton"]) {
        UIButton *theButton = (UIButton *)view;         

        [theButton setSelected:NO];

    }
}
于 2013-07-18T17:45:23.650 回答
0

只是一个额外的答案,以防(像我一样)围绕按钮的选定状态已经有大量的逻辑(父类基于此为视图提供状态)。如果您需要按钮以带有图像的未选择状态开始并使用无图像将状态更改为已选择,那么您可以像这样将生成的空白图像添加到选定状态。

            //Make a stupid blank image because setting to nil on a state doesn't work.
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(23, 23), NO, 0.0);
            UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            UIImage *buttonNonSelectedImage = [UIImage imageNamed:@"video-slomo"];
            [self.myButton setBackgroundImage:buttonNonSelectedImage forState:UIControlStateNormal];
            [self.mybutton setBackgroundImage:blank forState:UIControlStateSelected];
            [self.myButton addTarget:self
                                    action:@selector(onMyButtonPress)
                          forControlEvents:UIControlEventTouchUpInside];
            self.myButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            [self.playRateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [self.view addSubview:self.myButton];

然后,在您的按钮操作中,您可以更改状态(在我的情况下添加文本)。

- (void)onMyButtonPress
{
    if (self.myButton.isSelected) {
        [self.myButton setSelected:NO];
    }
    else {
        [self.myButton setTitle:@"Selected"]
                              forState:UIControlStateSelected];
        [self.myButton setSelected:YES];
    } 
}

接受的答案在大多数情况下都有效,我只需要选择状态来获取其他跟踪信息。快乐编程。

于 2017-04-05T20:13:11.607 回答