首页 文章

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

提问于
浏览
2

我正在尝试以编程方式创建UIButton . 我有一个名为“addCash”的按钮(已在界面构建器中创建),点击此按钮后我想要另一个按钮动态显示 . 在viewDidLoad中完成此按钮时工作正常,但这不是我想要的,因为在创建新按钮之前需要点击“addCash” . 这就是我到目前为止......

-(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]; 
}

1 回答

  • 1

    如果你普通添加了很多按钮,请使用 UITableView with custom cell . 如果您只是添加新按钮进行查看,则会出现性能问题 - 所有按钮都将同时加载 . UITableView 可以管理这种情况并卸载未使用的单元格 .

相关问题