UIButton setHidden现在不工作了?

时间:2013-03-24 16:27:00

标签: xcode ios6 uibutton

我在方法中设置了10个左右的按钮,如下所示:

@implementation MyViewController
UIButton *originalButton;

etc...

- (void)setupButtons
{
    originalButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [originalButton addTarget:self action:@selector(originalButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
    originalButton.frame = CGRectMake(20.0, 30.0, 100.0, 39.0);
    UIImage *buttonImage = [[UIImage imageNamed:@"originalreg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
    UIImage *buttonImageHighlight = [[UIImage imageNamed:@"originalregblue.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
    [originalButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [originalButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
    [self.view addSubview:originalButton];

    etc…
}

我决定将公共代码拉入另一种提高效率的方法:

- (void)setupButton:(UIButton *)myButton withSelector:(SEL)selector withX:(CGFloat)x withY:(CGFloat)y withRegImage:(NSString *)regImage withHighlightImage:(NSString *)highlightImage
{
    myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    myButton.frame = CGRectMake(x, y, 100.0, 39.0);
    UIImage *buttonImage = [[UIImage imageNamed:regImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
    UIImage *buttonImageHighlight = [[UIImage imageNamed:highlightImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
    [myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [myButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
    [self.view addSubview:myButton];
}

...并将其称为:

- (void)setupButtons
{
    [self setupButton:originalButton withSelector:@selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"originalreg.png" withHighlightImage:@"originalregblue.png"];

    etc...
}

这一切工作除外,我的一个按钮用于隐藏所有其他按钮。在原始设置中,按“隐藏按钮”按钮导致其他按钮被隐藏。现在,他们仍然在屏幕上。这是代码:

[self setupButton:hideButtonsButton withSelector:@selector(hideButtonsButtonWasPressed:) withX:20.0 withY:530.0 withRegImage:@"hidebuttonsreg.png" withHighlightImage:@"hidebuttonsregblue.png"];

- (void)hideButtonsButtonWasPressed:(id)sender
{
    // hide the buttons
    originalButton.hidden = YES;
    originalButton.enabled = NO;

    etc…
}

我已经确认正在调用此方法并且正在执行setHidden/setEnabled调用。

任何指针都感激不尽! 贝。

1 个答案:

答案 0 :(得分:2)

因为你的方法一直使用单个实例。看一下初始化的第一行

myButton = [UIButton buttonWithType:UIButtonTypeCustom];

替换此行
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

每次都会返回按钮的新实例。

你已经宣布按钮实例如下,对吧。

IBOutlet UIButton *btn1, *btn2, *btn3;

现在使用您之前创建的方法创建一个新按钮,只需将该按钮指定给受尊重的按钮对象

btn1 = [self setupButton:originalButton withSelector:@selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"originalreg.png" withHighlightImage:@"originalregblue.png"];

btn2 = [self setupButton:originalButton withSelector:@selector(duplicateButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"duplicatereg.png" withHighlightImage:@"originalregblue.png"];

btn3 = [self setupButton:originalButton withSelector:@selector(olderButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:@"olderreg.png" withHighlightImage:@"originalregblue.png"];