设置enabled = NO后,iPhone UIButton不会禁用

时间:2010-04-08 16:06:37

标签: iphone xcode uibutton

我正在从斯坦福大学班级做HelloPoly示例并尝试在适当的时候禁用增加/减少按钮

#import <Foundation/Foundation.h>
#import "PolygonShape.h"

@interface Controller : NSObject {
    IBOutlet UIButton *decreaseButton;
    IBOutlet UIButton *increaseButton;
    IBOutlet UILabel *numberOfSidesLabel;
    IBOutlet UILabel *nameLabel;
    IBOutlet UILabel *angleLabel;
    IBOutlet UILabel *minSidesLabel;
    IBOutlet UILabel *maxSidesLabel;

    IBOutlet PolygonShape *polygonShape;
}


-(IBAction)decrease:(id)sender;
-(IBAction)increase:(id)sender;
-(void)updateUI;

@end

然后在我的Controller.m中,没有对增加或减少按钮的影响

-(IBAction)decrease:(id)sender
{
    //NSLog(@"-");
    polygonShape.numberOfSides--;
    if (polygonShape.numberOfSides == polygonShape.minimumNumberOfSides)
        decreaseButton.enabled = NO;
    else 
        decreaseButton.enabled = YES;

    self.updateUI;

    increaseButton.enabled = NO;
    increaseButton.highlighted = YES;
    increaseButton.hidden = YES;

}

1 个答案:

答案 0 :(得分:2)

这就是我很久以前处理它的方式,比你的版本更冗长,但基本上和评论一样,检查你在IB的连接,并坚持使用YES / NO。

- (IBAction)decrease:(id)sender {
if ([shape numberOfSides] >= minNumberOfSides) {
    [shape setNumberOfSides:[shape numberOfSides]-1];
    NSLog(@"Decrease!");
}
[self updateInterface];
}

- (IBAction)increase:(id)sender {
if ([shape numberOfSides] <= maxMumberOfSides) {
    [shape setNumberOfSides:[shape numberOfSides]+1];
    NSLog(@"Increase!");
}
[self updateInterface];
}

- (void)updateInterface {
numberOfSidesLabel.text = [NSString stringWithFormat:@"%d", [shape numberOfSides]];
nameLabel.text = [NSString stringWithFormat:@"%@", [shape name]];

if ([shape numberOfSides] == minNumberOfSides) {
    decreaseButton.enabled = NO;
}
else {
    decreaseButton.enabled = YES;
}

if ([shape numberOfSides] == maxNumberOfSides) {
    increaseButton.enabled = NO;
}
else {
    increaseButton.enabled = YES;
}
}