IOS 代码创建控件,并有处理事件
2018-07-20 来源:open-open
@interface AppDelegate()
@property UILabel* show;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//创建一个UIViewController 对象
UIViewController* controller = [[UIViewController alloc] init];
//让该程序的窗口加载并显示 viewController 视图控制器关联的用户界面
self.window.rootViewController = controller;
//创建一个UIView 对象
UIView* rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//设置 controller 显示 rootView 控件
controller.view = rootView;
//创建一个圆角按钮
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置按钮的大小
button.frame = CGRectMake(120, 100, 80, 40);
//为按钮设置文本
[button setTitle:@"确定" forState:UIControlStateNormal];
//将按钮添加到 rootView 控件中
[rootView addSubview:button];
//创建一个 UILabel 对象
self.show = [[UILabel alloc] initWithFrame:CGRectMake(60, 40, 180, 30)];
// UILabel* show = [[UILabel alloc] initWithFrame:CGRectMake(60, 40, 180, 30)];
//将UILabel 添加到 rootView 控件中
[rootView addSubview: self.show];
//设置 UILabel 默认显示的文本
self.show.text = @"初始文本";
self.show.backgroundColor = [UIColor grayColor];
//为圆角按钮的触碰事件绑定事件处理方法
[button addTarget:self action:@selector(clickHandler:) forControlEvents:UIControlEventTouchUpInside];
//将该 UIWindow 对象设为主窗口并显示出来
[self.window makeKeyAndVisible];
return YES;
}
- (void)clickHandler:(id)sender {
self.show.text = @"开始学习 IOS 吧!";
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:php生成折线图代码
下一篇:NSData使用与分析
最新资讯
热门推荐