简体   繁体   中英

Creating UIButton programmatically with custom class

I want to create 3-4 buttons programmatically but with a custom class and a specific value/key pair, but I struggle on how to do this exactly. The button I want to create must have a custom class named "AnswerButton". Adding a UIButton.tag shouldnt be a problem, so I can exactly tell what button has been clicked, right?

Here is the code I use to create buttons:

NSMutableArray *catNames;
catNames = [NSMutableArray arrayWithCapacity:5];
[catNames addObject:@"Boote"];
[catNames addObject:@"Gewässer"];
[catNames addObject:@"Technik"];
[catNames addObject:@"Krims Krams"];

[self dynamiclyCreateButtons:4 :catNames];

- (void)dynamiclyCreateButtons:(int)howMany :(NSMutableArray*)catNames {
    float standard_btnHeight = 30.0;
    float standard_btnWidth = 200.0;
    CGFloat p = 120;

    for(int i = 0; i != howMany; i++){
        UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [catBtn setFrame:CGRectMake(0.0f, 0.0f, standard_btnWidth, standard_btnHeight)];
        [catBtn setCenter:CGPointMake(100.0f, p)];
        [catBtn setTitle:[catNames objectAtIndex:i] forState:UIControlStateNormal];
        [catBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:catBtn];
        p=p+40;
        catBtn = nil;
    }
}

//Edit What I mean by custom class: https://i.stack.imgur.com/8r2oz.png

//Edit 2 (The correct answer, since I cannot post an answer myself to point it out better)

Just here to point it out and find the answer easier: Answer by Greg is correct. You shouldn't create the buttons with the default class but with your custom class instead:

Replace

UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

with

AnswerButton *catBtn = [AnswerButton buttonWithType:UIButtonTypeRoundedRect];

Dont forget to import the class!

Try This...

-(void)dynamiclyCreateButtons:(int)howMany :(NSMutableArray*)catNames {
    float standard_btnHeight = 30.0;
    float standard_btnWidth = 200.0;
    CGFloat p = 120;


    for(int i = 0; i != howMany; i++){
        UIButton *catBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [catBtn setFrame:CGRectMake(0.0f, 0.0f, standard_btnWidth, standard_btnHeight)];
        [catBtn setCenter:CGPointMake(100.0f, p)];
        [catBtn setTag:i];
        [catBtn setTitle:[catNames objectAtIndex:i] forState:UIControlStateNormal];
        [catBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:catBtn];
        p=p+40;
        catBtn = nil;
    }
}
- (IBAction)buttonClicked:(id)sender
{
    NSLog(@"Button.Tag = %d",[sender tag]);
}

EDIT: If you want to use the custom class, you have to create a class inherited from UIButton . Then import this class in your view controller.

After that replace UIButton with YourCustomButton like..

YourCustomButton *catBtn = [YourCustomButton buttonWithType:UIButtonTypeRoundedRect];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM