0

好吧,我正在尝试一种快速的方法来创建按钮并对其进行编辑。

我已经声明了按钮

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;不起作用。

任何帮助是极大的赞赏。

4

1 回答 1

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];
于 2013-08-27T21:21:24.840 回答