IOS 代碼創建控件,并有處理事件

nd4b 9年前發布 | 2K 次閱讀 C/C++ IOS

    @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;  
}  </pre> 


    - (void)clickHandler:(id)sender {  
        self.show.text = @"開始學習 IOS 吧!";  
    }  

 本文由用戶 nd4b 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!