码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 代码创建控件,并有处理事件

时间:2015-01-21 22:35:50      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

@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 吧!";
}


IOS 代码创建控件,并有处理事件

标签:

原文地址:http://blog.csdn.net/wide288/article/details/42978519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!