我的iOS學習歷程 - MVC模式

什麼是MVC模式呢?就是將視圖數據,視圖,以及一些方法分別封裝在Model,View,Controller中,就叫MVC

我們先創建一個RootViewController類:
(這裏跟上文的類相關聯,可以兩文章一起看)

每一個視圖控制器 都自帶一個 view,並且這個view跟屏幕一樣大小

@implementation RootViewController

//  這個方法是加載視圖的
//  並且加載的是自己自帶的view
- (void)loadView {
    //  用上文的loginView 替換 控制器的View
    LoginView *loginView = [[LoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    loginView.backgroundColor = [UIColor whiteColor];
    loginView.userNameLTView.label.text = @"賬號";
    loginView.userNameLTView.textField.placeholder = @"請輸入賬號";
    loginView.userNameLTView.textField.tag = 100;
    //  給userNameLTView.textField設置代理
    loginView.userNameLTView.textField.delegate = self;
    loginView.passwordLTView.label.text = @"密碼";
    loginView.passwordLTView.textField.placeholder = @"請輸入賬號";
    loginView.passwordLTView.textField.tag = 101;
    //  給passwordLTView.textField設置代理
    loginView.passwordLTView.textField.delegate = self;

這裏主要是想要將方法全部寫在controller裏面 數據全在loginView裏,接下來的代碼中穿插了怎麼判斷屏幕橫屏豎屏:
這是自定義視圖loginView

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kHighDistance  30 //  行間距
#define kButtonDistance 50  //  button間距
@implementation LoginView
- (void)dealloc {
    [_userNameLTView release];
    [_passwordLTView release];
    [_registerButton release];
    [_findPasswordButton release];
    [_loadingButton release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.userNameLTView = [[LTView alloc]initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 100, 300, 50)];
        [self addSubview:self.userNameLTView];
        [_userNameLTView release];
        self.passwordLTView = [[LTView alloc]initWithFrame:CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kHighDistance, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)];
        [self addSubview:self.passwordLTView];
        [self.passwordLTView release];
        //  循環創建button
        for (int i = 0; i < 3; i ++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake(50 + (i * 100), 300, 80, 80) ;
            button.backgroundColor = [UIColor whiteColor];
            //  加標籤 方便取出button 與屬性的button 相對應
            button.tag = i + 100;
            [self addSubview:button];
        }
        //  屬性與循環的Button進行關聯
        self.loadingButton = (UIButton *)[self viewWithTag:100];
        self.findPasswordButton = (UIButton *)[self viewWithTag:101];
        self.registerButton = (UIButton *)[self viewWithTag:102];
    }
return self;
}
UIImage *image = [UIImage imageNamed:@"DB8E93D2-FA0D-4DE6-A57C-A6A6A1B7427A"];
    [loginView.loadingButton setTitle:@"登陸" forState:(UIControlStateNormal)];
    [loginView.loadingButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [loginView.loadingButton setBackgroundImage:image forState:(UIControlStateHighlighted)];
    [loginView.registerButton setTitle:@"註冊" forState:(UIControlStateNormal)];
    [loginView.registerButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [loginView.registerButton setBackgroundImage:image forState:(UIControlStateHighlighted)];
    [loginView.findPasswordButton setTitle:@"找回密碼" forState:(UIControlStateNormal)];
    [loginView.findPasswordButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [loginView.findPasswordButton setBackgroundImage:image forState:(UIControlStateHighlighted)];


    //  幫系統給self.view 賦值
    loginView.tag = 10000;
    self.view = loginView;
    [loginView release];
}

重新佈局子視圖 layoutSubviews
frame發生變化的時候 會觸發該方法

    //  因爲不知道父類的方法做了什麼
    //  所以在重寫的時候 先調用一下父類的方法
    //  然後再寫咱們自己的
- (void)layoutSubviews {
- [super layoutSubviews];
    //  frame發生變化 相當於橫屏了
    //  這時需要重新佈局
    //  判斷是豎屏還是橫屏
    //  1.把應用程序取出來
    //  2.判斷一下當前應用程序 屏幕的朝向
    //  取出應用程序 sharedApplication 單例方法的命名規範share+
    UIApplication *app = [UIApplication sharedApplication];
    //  判斷方向 statusBarOrientation
    if(app.statusBarOrientation == UIInterfaceOrientationPortrait || app.statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
       self.passwordLTView.frame = CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kHighDistance, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height);
    }else {
        self.passwordLTView.frame = CGRectMake(self.userNameLTView.frame.origin.x + self.userNameLTView.frame.size.width + 20, self.userNameLTView.frame.origin.y, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height);
    }
}

接下來就是把方法寫到上面controller控制器裏:

//  視圖已經加載完成
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // 更改一下自身view的顏色
    self.view.backgroundColor = [UIColor whiteColor];
    //  控制器中寫邏輯部分
    //  給button添加一個點擊方法
    LoginView *loginView = (LoginView *)self.view;
    [loginView.loadingButton addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];

}
//  鍵盤迴彈 textFieldShouldReturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

/*
 屏幕旋轉
 屏幕橫屏時 橫屏佈局
 屏幕豎屏時 豎屏佈局
 1.允許屏幕旋轉
 2.指定屏幕旋轉的方向
 3.找到旋轉觸發的方法
 4.判斷屏幕方向 更改佈局
 5.測試一下
 */

//  1.允許屏幕旋轉 shouldAutorotate
- (BOOL)shouldAutorotate {
    return YES;
}

//  2.指定屏幕旋轉的方向 supportedInterfaceOrientations
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

本章最重要的是學會使用MVC這種模式,將數據,視圖以及方法分開封裝,提高代碼利用率和滿足低偶爾性!希望對你們有幫助

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