将背景图像设置为nil后,iOS UIButton消失

时间:2013-07-17 18:00:33

标签: ios objective-c ios6 uiviewcontroller uibutton

我有一个没有标题或初始背景图像的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];

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

3 个答案:

答案 0 :(得分:1)

为什么不改变按钮的状态,而不是改变UIControlStateNormal中按钮的背景图像? 如果以编程方式创建按钮,则只需添加行

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

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

[btn setSelected:YES];

并将其设置回默认外观:

[btn setSelected:NO];

如果你在XIB中创建了按钮,你可以通过将状态配置更改为Selected来设置状态图像,并在那里设置图像。

答案 1 :(得分: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];

    }
}

答案 2 :(得分: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];
    } 
}

在大多数情况下,接受的答案有效,我只需要其他跟踪信息的选定状态。快乐的节目。