iOS UI學習筆記(六)UIViewController

之前的學習,我們把視圖寫在AppDelegate裏,從今開始,一律寫在試圖控制器裏。

首先,在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];
    [self.window makeKeyAndVisible];
    //初始化根視圖控制器
    RootViewController_WYX *rootViewController = [[RootViewController_WYX alloc]init];
    self.window.rootViewController = rootViewController;
    
    /*
     模擬rootViewcontroller的setter方法
     -(void)setRootViewController:(UIViewController*)rootViewController{
         
       if (_rootViewController!=rootViewController){
          [_rootViewController release];
          _rootViewController = rootViewController;
          //self 代表的是window
          [self addSubView:rootViewController];
       }
     }
     
     */
    return YES;
}

這是將控制器裏的視圖加載到window上。

然後創建UIViewController

RootViewController_WYX.m
這裏創建的是一個類,繼承於UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // Do any additional setup after loading the view from its nib.
#pragma mark - 登錄界面  
    //常見登錄界面的背景
    UIView *loginView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //設置頂部標題
    UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth(loginView.frame), 44)];
    [titlelabel setText:@"登 錄"];
    //設置背景顏色
    [titlelabel setBackgroundColor:[UIColor lightGrayColor]];
    //標題顏色
    [titlelabel setTextColor:[UIColor whiteColor]];
    //字體
    [titlelabel setFont:[UIFont fontWithName:@"Optima-Bold" size:25]];
    //對齊方式
    [titlelabel setTextAlignment:NSTextAlignmentCenter];
    //顯示
    [loginView addSubview:titlelabel];
    //設置背景顏色
    [loginView setBackgroundColor:[UIColor whiteColor]];
    //    [loginView setUserInteractionEnabled:YES];
    //創建lable和text,有規律的控件,所以可以用for循環創建
    NSArray *labelTextNameArray = [NSArray arrayWithObjects:@"用戶名",@"密碼", nil];
    NSArray *textfieldNameArray = [NSArray arrayWithObjects:@"請輸入用戶名",@"請輸入密碼", nil];
    for (int i = 0; i < 2; i ++) {
        //每循環一次創建一行控件,一行包括label和textfiled
        UILabel *myLable = [[UILabel alloc]initWithFrame:CGRectMake(kLable_X, 200+i*50, kLable_Width, 30)];
        [myLable setText:labelTextNameArray[i]];
        //右對齊
        [myLable setTextAlignment:NSTextAlignmentRight];
        UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(kLable_X + kLable_Width + 20, 200+i*50, kLable_Width*1.6, 30)];
        //邊界風格
        [myTextField setBorderStyle:UITextBorderStyleRoundedRect];
        //設置佔位符
        [myTextField setPlaceholder:textfieldNameArray[i]];
        //設置代理
        [myTextField setDelegate:self];
        //設置tag值,方便touch事件管理
        [myTextField setTag:1010+i];
        [loginView addSubview:myLable];
        [loginView addSubview:myTextField];
    }
    UITextField *pwdField = (UITextField*)[loginView viewWithTag:1011];
    [pwdField setSecureTextEntry:YES];
    NSArray *btnNameArray = [NSArray arrayWithObjects:@"登錄",@"找回密碼",@"註冊", nil];
    for (int i = 0; i < 3 ; i++) {
        UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [myBtn setFrame:CGRectMake(50+i*(70+40), 320, 70, 30)];
        //設置標題
        [myBtn setTitle:btnNameArray[i] forState:UIControlStateNormal];
        //設置tag值
        [myBtn setTag:1000+i];
        //設置字體顏色
        [myBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //設置背景顏色
        [myBtn setBackgroundColor:[UIColor colorWithRed:0.0 green:128/255.0 blue:255.0 alpha:1]];
        //圓角按鈕
        [myBtn.layer setMasksToBounds:YES];
        [myBtn.layer setCornerRadius:5.0];
        //添加按鈕
        [loginView addSubview:myBtn];
    }
    [self.view addSubview:loginView];
    UIButton *registerBtn = (UIButton *)[loginView viewWithTag:1002];
    [registerBtn addTarget:self action:@selector(jumpToRegisterViewAction:) forControlEvents:UIControlEventTouchUpInside];
//    UIButton *loginBtn = (UIButton *)[loginView viewWithTag:1000];
//    [loginBtn addTarget:self action:@selector(jumpTologinViewAction:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *findScretBtn = (UIButton *)[loginView viewWithTag:1001];
    [findScretBtn addTarget:self action:@selector(jumpToFindPWDViewAction:) forControlEvents:UIControlEventTouchUpInside];
    //調試語句
    NSLog(@"視圖加載完畢");
#pragma mark - 上課實驗
    
}


-(void)viewDidLoad是說在視圖加載完畢後實現的內容。

所以要實現的視圖代碼寫在這裏。上述代碼生成了這樣一個頁面:


那麼注意在上面寫了button的回調時間,注意實現。

在這裏實現了註冊按鈕跳轉註冊頁面和找回密碼按鈕跳轉找回密碼界面。

代碼如下:

//點擊註冊按鈕的方法
-(void)jumpToRegisterViewAction:(UIButton*)sender{
    //跳轉註冊的視圖控制器,這種方式叫做模態
    //初始化註冊視圖控制器
    RegisterViewController_WYX *registerVC = [[RegisterViewController_WYX alloc]init];
    /*
     第一個參數:即將出現的視圖
     第二個參數:是否需要動畫
     第三個參數:是一個block參數,目前可以理解爲代碼片。其作用是跳轉完成之後,所執行的操作
     */
    registerVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:registerVC animated:YES completion:^{
        NSLog(@"sb");
    }];
}

-(void)jumpToFindPWDViewAction:(UIButton*)sender{
    PasswordViewController_WYX *passWord = [[PasswordViewController_WYX alloc]init];
    [self presentViewController:passWord animated:YES completion:^{     
    }];
}

注意跳轉頁面的參數傳遞,最後一個代碼片爲跳轉完成後執行的代碼。

同樣要實現這些功能,首先要創建其他兩個頁面,並將其頭文件導入到根視圖控制器的.m文件纔可以實現。

另外兩個界面的代碼就不上了,效果圖如下:


愛這裏重點記住幾個方法:

#pragma mark - 視圖出現和消失是,所執行的方法。
//任何時候在視圖即將出現的時候,都會執行方法。
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   //調試語句//
    NSLog(@"%s--視圖即將出現",__FUNCTION__);
}

//視圖已經出現
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%s---視圖已經出現",__FUNCTION__);
}
//視圖即將消失
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     NSLog(@"%s---視圖即將消失",__FUNCTION__);
}
//視圖已經消失
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
     NSLog(@"%s---視圖已經消失",__FUNCTION__);

}
#pragma mark - 視圖釋放的時候,執行的方法
-(void)dealloc{
    //ARC下,不需要寫superdealloc;MRC下需要寫[super dealloc]
    
}

這幾個方法在視圖出現和消失的前後都會調用,可以在視圖控制器裏重寫,重寫之後就可已完成想要的功能了。

比方說重新打開時刷新頁面等等。

之前學習的代理,也可以在這裏實現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章