以编程方式添加按钮方法不起作用?

时间:2013-08-27 21:15:20

标签: objective-c uibutton

好吧,我正在尝试快速创建按钮& amp;编辑它们。

我已宣布按钮

UIButton *button1, *button2;

之后我制作了创建按钮的方法。

-(void)addButton:(NSString *)name :(NSString *)buttonId :(UIButton *)button {
    int height;
    height = buttonId.intValue;
    height = height *45;

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:name forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 120+height, 160.0, 40.0);
    [self.view addSubview:button];

}

-viewDidLoad内我添加了调用方法

[self addButton:@"Button one!" : @"1" : button1];

现在一切正常,按钮显示一切。但是,如果我尝试更改按钮的alpha或隐藏它,它就不起作用。

,例如button1.alpha = 0.1;不起作用。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的方法签名应如下所示:

-(void)addButton:(NSString *)name :(NSString *)buttonId :(UIButton **)outButton {

或者更好,

- (UIButton *)addButtonWithTitle:(NSString *)title index:(NSInteger)buttonID {

在前一种情况下,实现类似于

…
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
*outButton = button;

在后者中,

int height = buttonID * 45;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
…
return button;

然后你最初用它来打电话,比方说,

button1 = [self addButtonWithTitle:@"Button one!" index:1];
相关问题