博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义视图,视图控制器的生命周期
阅读量:5164 次
发布时间:2019-06-13

本文共 4791 字,大约阅读时间需要 15 分钟。

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 *)textField40 {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 *)nibBundleOrNil10 {11     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {12         NSLog(@"这个方法被调用了");13         NSLog(@"%s",__FUNCTION__);//打印当前被调用的方法的名称14     }15     return self;16    17 }18 //加载视图,用以代码布局的时候被调用,当视图第一次载入的时候调用的方法,企业级的面试题之一,ViewController的生命周期,加载视图到内存再到显示再到视图,加载->内存->显示->视图->消失->内存->dealloc19 -(void)loadView20 {21     [super loadView];22     //__LINE__,方法被调用时所在的代码行数23     NSLog(@"%s %d",__FUNCTION__,__LINE__);24 }25 //加载完毕26 -(void)viewDidLoad27 {28     NSLog(@"%s",__FUNCTION__);29     //self.view.backgroundColor = [UIColor redColor];30     //背景图,ViewController自带一个view31     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)animated65 {66     NSLog(@"%s, %d",__FUNCTION__,__LINE__);67 }68 //视图已经出现69 -(void)viewDidAppear:(BOOL)animated70 {71     NSLog(@"%s",__FUNCTION__);72 }73 //视图将要消失74 -(void)viewWillDisappear:(BOOL)animated75 {76     NSLog(@"%s",__FUNCTION__);77 }78 //视图已经消失79 -(void)viewDidDisappear:(BOOL)animated80 {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

 

转载于:https://www.cnblogs.com/DevinSMR/p/5164420.html

你可能感兴趣的文章
原型设计工具
查看>>
windows下的C++ socket服务器(4)
查看>>
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
对Vue为什么不支持IE8的解释之一
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>