• 自定义视图,视图控制器的生命周期


    1.自定义视图

     1 #import "MyView.h"
     2 
     3 @implementation MyView
     4 
     5 
     6 //重写父类的初始化方法
     7 
     8 -(instancetype)initWithFrame:(CGRect)frame
     9 {
    10     if(self = [super initWithFrame:frame]){
    11      //切记引用,如果不写的话则没办法调用
    12         [self setView_up];
    13     }
    14     return self;
    15 }
    16 //私有方法布局
    17 -(void)setView_up{
    18 
    19     //设置位置
    20     self.leftLable =[[UILabel alloc]initWithFrame:CGRectMake(20, 20, 80, 40)];
    21     //设置颜色
    22     //self.leftLable.backgroundColor = [UIColor magentaColor];
    23     //设置字体
    24     self.leftLable.text = @"用户名";
    25     self.leftLable.font = [UIFont systemFontOfSize:16];
    26     //对齐方式-----居中
    27     self.leftLable.textAlignment = NSTextAlignmentCenter;
    28     //添加
    29     [self addSubview:self.leftLable];
    30     
    31     self.rightTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.leftLable.frame.origin.x + self.leftLable.frame.size.width + 20,self.leftLable.frame.origin.y,self.frame.size.width - self.leftLable.frame.size.width - 40,self.leftLable.frame.size.height)];
    32     self.rightTextField.placeholder = @"请输入用户信息";
    33     //self.rightTextField.backgroundColor = [UIColor grayColor];
    34     self.rightTextField.borderStyle = 3;
    35     self.rightTextField.delegate = self;    
    36     [self addSubview:self.rightTextField];
    37     
    38 }
    39 -(BOOL)textFieldShouldReturn:(UITextField *)textField
    40 {
    41     [textField resignFirstResponder];
    42     return YES;
    43 }
    44 
    45 @end

    2.ViewController的声明周期

     1 #import "ViewController.h"
     2 #import "MyView.h"
     3 @interface ViewController ()
     4 //@property (strong,nonatomic) MyView *myView;
     5 @end
     6 
     7 @implementation ViewController
     8 //系统封装了了初始化方法,每次运行都会先调用初始化方法
     9 -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    10 {
    11     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    12         NSLog(@"这个方法被调用了");
    13         NSLog(@"%s",__FUNCTION__);//打印当前被调用的方法的名称
    14     }
    15     return self;
    16    
    17 }
    18 //加载视图,用以代码布局的时候被调用,当视图第一次载入的时候调用的方法,企业级的面试题之一,ViewController的生命周期,加载视图到内存再到显示再到视图,加载->内存->显示->视图->消失->内存->dealloc
    19 -(void)loadView
    20 {
    21     [super loadView];
    22     //__LINE__,方法被调用时所在的代码行数
    23     NSLog(@"%s %d",__FUNCTION__,__LINE__);
    24 }
    25 //加载完毕
    26 -(void)viewDidLoad
    27 {
    28     NSLog(@"%s",__FUNCTION__);
    29     //self.view.backgroundColor = [UIColor redColor];
    30     //背景图,ViewController自带一个view
    31     UIView *backView = [[UIView alloc] initWithFrame:self.view.frame];
    32     backView.backgroundColor = [UIColor greenColor];
    33     [self.view addSubview:backView];
    34     //创建MyView对象
    35     MyView *userName = [[MyView alloc] initWithFrame:CGRectMake(10, 10, 300, 70)];    
    36     userName.leftLable.text = @"用户名";
    37     //userName.backgroundColor = [UIColor magentaColor];
    38     NSLog(@"%@",NSStringFromCGRect(userName.rightTextField.frame));
    39     [backView addSubview:userName];
    40     
    41     userName.rightTextField.placeholder = @"请输入用户名";
    42     
    43     MyView *userPassword = [[MyView alloc] initWithFrame:CGRectMake(userName.frame.origin.x, userName.frame.origin.y + 100, userName.frame.size.width, userName.frame.size.height)];
    44     userPassword.leftLable.text = @"密码";
    45     userPassword.rightTextField.placeholder = @"请输入密码";
    46     userPassword.rightTextField.keyboardType =  UIKeyboardTypeASCIICapable;
    47     userPassword.rightTextField.secureTextEntry = YES;
    48     //userPassword.backgroundColor = [UIColor magentaColor];
    49     [backView addSubview:userPassword];
    50     
    51     MyView *userPhone = [[MyView alloc] initWithFrame:CGRectMake(userPassword.frame.origin.x, userPassword.frame.origin.y + 100, userName.frame.size.width, userName.frame.size.height)];
    52     userPhone.leftLable.text = @"手机号";
    53     userPhone.rightTextField.placeholder = @"请输入手机号";
    54     userPhone.rightTextField.keyboardType =  UIKeyboardTypeASCIICapable;
    55     [backView addSubview:userPhone];
    56     
    57     MyView *userEmail = [[MyView alloc] initWithFrame:CGRectMake(userPhone.frame.origin.x, userPhone.frame.origin.y + 100, userName.frame.size.width, userName.frame.size.height)];
    58     userEmail.leftLable.text = @"邮箱";
    59     userEmail.rightTextField.placeholder = @"请输入邮箱号";
    60     userEmail.rightTextField.keyboardType =  UIKeyboardTypeASCIICapable;
    61     [backView addSubview:userEmail];
    62     }
    63 //视图将要显示
    64 -(void)viewWillAppear:(BOOL)animated
    65 {
    66     NSLog(@"%s, %d",__FUNCTION__,__LINE__);
    67 }
    68 //视图已经出现
    69 -(void)viewDidAppear:(BOOL)animated
    70 {
    71     NSLog(@"%s",__FUNCTION__);
    72 }
    73 //视图将要消失
    74 -(void)viewWillDisappear:(BOOL)animated
    75 {
    76     NSLog(@"%s",__FUNCTION__);
    77 }
    78 //视图已经消失
    79 -(void)viewDidDisappear:(BOOL)animated
    80 {
    81     NSLog(@"%s",__FUNCTION__);
    82 }
    83 
    84 
    85 //内存警告里什么都不要写
    86 - (void)didReceiveMemoryWarning {
    87     [super didReceiveMemoryWarning];
    88     // Dispose of any resources that can be recreated.
    89 }
    90 
    91 @end
  • 相关阅读:
    System.Configuration引用后ConfigurationManager方法用不了
    HTTP 错误 500.23
    slide ——首尾相接の平滑切换效果
    函数式编程初探之回调
    Call & Apply. It's easy!
    【W3C】 CSS3选择器
    再谈原型和原型链
    ECMA学习小结(3)——constructor 和 prototype
    ECMA学习小结(2)——一切皆对象
    ECMA学习小结(1)——什么是ECMAScript,JAVAScript与ECMA的关系
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5164420.html
Copyright © 2020-2023  润新知