简体   繁体   中英

Tap UIButton to create another UIButton programmatically

I'm trying to create a UIButton programmatically. I have a button called "addCash" (which was already created in interface builder), upon tapping this button I want another button to dynamically appear. This button works fine when done in viewDidLoad, but this is not what I want as "addCash" needs to be tapped before this new button is to be created. This is what I have so far...

-(IBAction) addCash{
UIButton *theButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
theButton.frame = CGRectMake(80, 50, 150, 40);
[theButton setTitle:@"title" forState:UIControlStateNormal];
[theButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
[theButton setBackgroundImage:[UIImage imageNamed:@"edit.jpg"] forState:UIControlStateNormal];

[self.view addSubview:theButton]; 
}

If you plain add a lot of buttons use UITableView with custom cell . If you will just add new buttons to view you will get a performance issue - all you buttons will be loaded simultaneously. UITableView can manage this situation and unload unused cell's.

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