iOS App开发UIButton事件方法代码添加指南

事件方法代码添加方式
在iOS开发中,UIButton控件可以通过代码方式添加点击事件。以下是一个示例代码:

objc
UIButton *addedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addedButton.frame = CGRectMake(240, 15, 60, 20);
addedButton.titleLabel.font = [UIFont systemFontOfSize:12.0f];
[addedButton addTarget:self action:@selector(addiPhoneNumber) forControlEvents:UIControlEventTouchUpInside];
[addedButton setTitle:@"添加" forState:UIControlStateNormal];
[self.view addSubview:addedButton];


代码说明
- UIButton *addedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 创建一个圆角按钮。
- addedButton.frame = CGRectMake(240, 15, 60, 20); 设定按钮的布局位置和大小。
- addedButton.titleLabel.font = [UIFont systemFontOfSize:12.0f]; 设置按钮字体大小。
- [addedButton addTarget:self action:@selector(addiPhoneNumber) forControlEvents:UIControlEventTouchUpInside]; 为按钮添加点击事件方法addiPhoneNumber
- [addedButton setTitle:@"添加" forState:UIControlStateNormal]; 设置按钮显示文字。
- [self.view addSubview:addedButton]; 将按钮添加到当前视图。

通过以上代码,UIButton控件得以成功创建并添加了点击事件。该方法适用于需要动态添加按钮的情况,提供了灵活的UI设计。

ppt 文件大小:15.38MB