您的位置:首页 > 移动开发 > IOS开发

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

2015-01-21 21:10 429 查看
@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 吧!";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: