随意细解:UI -- 自定义视图、视图控制器

来源:互联网 发布:淘宝靠谱的宠物药店 编辑:程序博客网 时间:2024/06/06 19:13

自定义视图

自定义label-textField视图

⾃⼰组合⽽出的新的视图。把Label和Textfield封装到LTView中,在⼀定程度上简化了我们的代码,提高工作效率,大大提高代码的复用性。(⾼质量代码特点:可复⽤,可移植,精炼等。)

登录界⾯有个特点:每个UITextField都有⼀个UILabel修饰。我们需要对LTView中的Label或者Textfield进⾏⼀定的控制,⽐如:设置Label的text,获取Textfield的text,给Textfield指定delegate,设置textColor等。

自定义视图步骤

1、创建⼀个UIView⼦类
2、在类的初始化⽅法中添加⼦视图
3、类的.h⽂件提供⼀些接⼝(⽅法),便于外界操作⼦视图。

代码演示:

  1. 在LTView.h中声明属性。方便外部取出

    @property (nonatomic, retain) UILabel *label;@property (nonatomic, retain) UITextField *textField;
  2. 在LTView.m中重写初始化方法

    //在MRC模式中需要重写dealloc方法    - (void)dealloc    {            [self.label release];            [self.textField release];            [super dealloc];        }    // 重写初始化方法,在初始化LTView的同时把label也加进去    - (instancetype)initWithFrame:(CGRect)frame    {         self = [super initWithFrame:frame];        if (self) {            // 动态获取宽度、高度            CGFloat width = frame.size.width;            CGFloat height = frame.size.height;            // 根据上面的宽度,添加label            self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width / 3, height)];          //  self.label.backgroundColor = [UIColor cyanColor];            [self addSubview:self.label];            [self.label release];            // 动态获取宽度            CGFloat x = width / 3 ;            CGFloat w = width * 2 / 3;            self.textField = [[UITextField alloc]initWithFrame:CGRectMake(x, 0, w, height)];          //  self.textField.backgroundColor = [UIColor greenColor];            self.textField.borderStyle = UITextBorderStyleRoundedRect;            [self addSubview:self.textField];            [self.textField release];        }    return self;    }
  3. 在View中可以直接使用LTView

视图控制器指定自定义View

UIViewController:视图控制器,是MVC设计模式的核⼼。控制视图显⽰,响应事件。分担AppDelegate的⼯作。实现模块独⽴,提⾼复⽤性。

视图控制器功能

控制视图⼤⼩变换、布局视图、响应事件。
检测以及处理内存警告。
检测以及处理屏幕旋转。
检测视图的切换。

视图控制器创建步骤

  1. 定义UIViewController的⼦类
  2. 创建视图控制器对象,作为window的根视图控制器
  3. 在viewDidLoad中使⽤默认创建好的视图对象view

代码演示:

  1. 引入头文件

    #import "RootViewController.h"
  2. 创建视图控制器对象,作为window的根视图控制器

     // 初始化控制器RootViewController *rootVC = [[RootViewController alloc]init];// 设置根视图控制器self.window.rootViewController = rootVC;// 释放[rootVC release];
  3. 在viewDidLoad中使⽤默认创建好的视图对象view

    - (void)viewDidLoad {    [super viewDidLoad];    }

视图控制器指定⾃定义view的创建步骤

注:视图控制器创建步骤如上所述
1. ⾃定义视图类继承UIView。在初始化⽅法中添加⼦视图控件。
2. 重写controller的loadView⽅法。创建⾃定义视图对象,并指定为controller的view。
3. 将⼦视图控件对象设置为⾃定义视图类的属性,在viewDidLoad⽅法中进⾏设置:添加action、设置delegate等等。
4. 在controller中添加按钮点击事件实现和代理⽅法的实现。

代码演示:
还是以LTView为例,新建登录视图

  1. ⾃定义视图类LoginView继承UIView。在初始化⽅法中添加⼦视图控件。

    -(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];        self.userNameLTView.label.text = @"用户名";        self.userNameLTView.textField.placeholder = @"请输入用户名:";        [self.userNameLTView release];        self.passwordLTView = [[LTView alloc]initWithFrame:CGRectMake((kScreenWidth - 300) / 2, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kRowHeight, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)];        self.passwordLTView.label.text = @"密码";        self.passwordLTView.textField.placeholder = @"请输入密码:";        [self addSubview:self.passwordLTView];        [self.passwordLTView release];        NSArray *arr = @[@"登录",@"注册",@"找回密码"];        // 循环button        for (int i = 0; i < 3; i++) {            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];            button.frame = CGRectMake(20 + (i * 100), 300, 90, 50);            [button setTitle:arr[i] forState:(UIControlStateNormal)];            [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];            [self addSubview:button];            // 加标签,方便取出button,与属性的button相对应            button.tag = 300 + i;        }        self.loginButton = (UIButton *)[self viewWithTag:300];        self.registerButton = (UIButton *)[self viewWithTag:301];        self.findPasswordButton = (UIButton *)[self viewWithTag:302];        }    return self;    }
  2. 重写Viewcontroller的loadView⽅法。创建⾃定义视图对象,并指定为controller的view。

    - (void)loadView    {        // 用loginView替换控制器的view        LoginView *loginView = [[LoginView alloc]initWithFrame:[UIScreen mainScreen].bounds];        // 帮系统给self.view赋值        self.view = loginView;        loginView.tag = 1000;        [loginView release];    }
  3. 将⼦视图控件对象设置为⾃定义视图类的属性,在viewDidLoad⽅法中进⾏设置:添加action、设置delegate等等。

    // 视图已经加载完成    - (void)viewDidLoad {        [super viewDidLoad];        // 更改自身view的颜色        self.view.backgroundColor = [UIColor grayColor];        // 给button添加一个点击方法        LoginView *loginView = (LoginView *)self.view;        [loginView.loginButton addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];        loginView.userNameLTView.textField.delegate = self;        loginView.passwordLTView.textField.delegate = self;    }
  4. 在ViewController中添加按钮点击事件实现和代理⽅法的实现。

    // 设置button的点击事件- (void)buttonClick:(UIButton *)button{    NSLog(@"我是登录按钮");}// return按钮的键盘回收的代理方法- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    return YES;}

检测屏幕旋转

屏幕横屏时,横屏布局。屏幕竖屏时,竖屏布局。

步骤

  1. 允许屏幕旋转
  2. 指定屏幕旋转的方向
  3. 找到旋转触发的方法
  4. 判断屏幕方向,更改布局

代码演示:

在ViewController.m中:

1.允许屏幕旋转

- (BOOL)shouldAutorotate{    return YES;}

2.指定屏幕旋转的方向

- (NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAll;}

3.旋转时触发的方法

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator    {        NSLog(@"%@",NSStringFromCGSize(size));    }

4.重新布局子视图

// frame发生变化的时候会触发layoutSubviews方法    -(void)layoutSubviews    {        // 因为不知道父类这个方法做了什么,所以需要在重写的时候,先调用一下父类的方法,然后再写自己的;        [super layoutSubviews];        // frame发生变化,相当于横屏了。这时需要重新布局        // 判断竖屏还是横屏        // 1.把应用程序取出来        // 2.判断当前应用程序 屏幕的朝向        // 取出应用程序  单例方法的命名规范share什么        UIApplication *app = [UIApplication sharedApplication];        // 判断方向        if (app.statusBarOrientation == UIInterfaceOrientationPortrait || app.statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {            NSLog(@"竖着的");             self.passwordLTView.frame = CGRectMake((kScreenWidth - 300) / 2, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kRowHeight, 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);        NSLog(@"横着的");        }     }

处理内存警告

控制器能监测内存警告,以便我们避免内存不够引起的crash。在定义的controller⼦类中重写didReceiveMemoryWarning⽅法。释放暂时不使⽤的资源。

// 接到内存警告触发的方法- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.    NSLog(@"内存警告了");    // 释放已经显示过的视图,并且不是当前显示的视图    // 如果视图正在被显示,那么这个视图的window属性不是空的    if ([self isViewLoaded] == YES && self.view.window == nil) {        // 把当前视图释放        self.view = nil;    }}

容器视图控制器

例: 创建两个视图控制器,把SecondViewController添加到RootViewController上,并在RootViewController上添加一个按钮,点击按钮,移除SecondViewController视图。

  1. 在RootViewController中引入SecondViewController的头文件

    #import "SecondViewController.h"
  2. 在RootViewController的viewDidLoad中添加子控制器SecondViewController

    - (void)viewDidLoad {        [super viewDidLoad];        // 如果你不想替换self.view,一般都写在viewDidLoad方法中        // 添加子控制器        self.secondVC = [[SecondViewController alloc]init];        // 把secondVC添加成子控制器,让rootVC去管理        [self addChildViewController:self.secondVC];        // 把secondVC.view添加到 rootVC.view上        [self.view addSubview:self.secondVC.view];        // 释放        [self.secondVC release];        self.view.backgroundColor = [UIColor redColor];        // 添加一个按钮,移除视图,移除子控制器        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];        button.frame = CGRectMake(100, 100, 100, 100);        button.backgroundColor = [UIColor blackColor];        // 添加到rootVC.view上        [self.view addSubview:button];        [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchDragInside)];    }
  3. 添加按钮点击事件,实现移除子视图控制器

    - (void)buttonClick:(UIButton *)button{    // 移除视图    [self.secondVC.view removeFromSuperview];    // 移除子控制器    [self.secondVC removeFromParentViewController];}

这时,子视图控制器SecondViewController的生命周期是:

  • 视图将要出现时的方法:

    -(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];}
  • 视图已经出现时的方法:

    -(void)viewDidAppear:(BOOL)animated{    [super viewWillAppear:animated];}
  • 视图将要消失时的方法:

    -(void)viewWillDisappear:(BOOL)animated{    [super viewWillAppear:animated];}
  • 视图已经消失时的方法:

    -(void)viewDidDisappear:(BOOL)animated{    [super viewWillAppear:animated];}

    注:可以在对应的方法中完成想要实现的功能。

0 0