简体   繁体   English

点击UIButton以编程方式创建另一个UIButton

[英]Tap UIButton to create another UIButton programmatically

I'm trying to create a UIButton programmatically. 我正在尝试以编程方式创建UIButton。 I have a button called "addCash" (which was already created in interface builder), upon tapping this button I want another button to dynamically appear. 我有一个名为“ addCash”的按钮(已在界面生成器中创建),点击此按钮后,我希望另一个按钮动态显示。 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. 在viewDidLoad中完成此按钮后,它可以正常工作,但这不是我想要的,因为在创建此新按钮之前,需要先轻按“ addCash”。 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 . 如果您简单地添加许多按钮,请使用带有自定义单元格的 UITableView 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. UITableView可以管理这种情况并卸载未使用的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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