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

0

DOCの回答を実装する最終的なコードは次のとおりです。Interface Builder でデフォルトと選択状態を設定します。

- (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 に答える