我的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这种模式,将数据,视图以及方法分开封装,提高代码利用率和满足低偶尔性!希望对你们有帮助