xcode 设置启用后 iPhone UIButton 不会禁用 = NO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2601495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:16:25  来源:igfitidea点击:

iPhone UIButton won't disable after setting enabled = NO

iphonexcodeuibutton

提问by chrisan

I'm doing the HelloPoly example from the Stanford class and trying to disable the increase/decrease buttons when appropriate

我正在做斯坦福课上的 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

and then in my Controller.m, none of the effects on the increase or decrease button take

然后在我的 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;

}

采纳答案by Eric Schweichler

This is how I handled it so long ago, a bit more verbose than your version but basically the same, as in the comment, check your connections in IB, and stick with YES/NO everywhere as well.

这就是我很久以前处理它的方式,比你的版本更冗长,但基本相同,如评论中所示,检查你在 IB 中的连接,并在任何地方都坚持是/否。

- (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;
}
}