UIViewController
先创建一个继承于UIViewController的类,我们可以利用这个类来进行视图之间的切换和布置.
简介UIViewController.
1.初始化方法,这个方法一般自己就调用了,不需要我们再额外调用,会初始化一些容器,比如数组,字典等
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
self.arr = [NSMutableArray array];
}
//打印出当前方法名
NSLog(@"%s",__FUNCTION__);
return self;
}
#pragma mark 如果想重写父类的方法,首先先用super去调用父类的方法.这样可以保证原功能不变,然后再方法里再写新添加的功能
- (void)loadView
{
//self.view 的加载
[super loadView];
NSLog(@"%s",__FUNCTION__);
}
//视图将要出现'
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"%s",__FUNCTION__);
}
//视图已经出现
#warning 这个方法是视图已经出现
//这是手写警告方法,用来方便查找
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%s",__FUNCTION__);
}
//视图将要消失
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];
NSLog(@”%s”,FUNCTION);
}
//视图已经消失
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@”%s”,FUNCTION);
}
//视图已经出现,通常使用的, 视图的创建和铺设都在viewDidLoad方法中进行
- (void)viewDidload{
[super viewDidLoad];
self.view.backgroundColor = [ UIColor yellowColor];
//铺三个textField
UITextField *textField1 = [UITextField alloc]initWithFrame:CGRectMake(100,200,150,40)];
textField1.layer.borderWidth = 1;
textField1.layer.cornerRedius = 10;
[self.view addSubview:textField1 ];
textField1/delegate = self;
[textField1 release];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 300, 150, 40)];
textField2.layer.borderWidth = 1 ;
textField2.layer.cornerRadius =10 ;
[self.view addSubview:textField2];
textField2.delegate = self;
[textField2 release];
UITextField *textField3 = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 40)];
textField3.layer.borderWidth = 1 ;
textField3.layer.cornerRadius = 10 ;
[self.view addSubview:textField3];
textField3.delegate = self;
[textField3 release];
//铺一个button
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 500, 50, 40);
[button setTitle:@”下一页” forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10 ;
button.backgroundColor = [UIColor cyanColor];
//上下移动textField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//只要输入框被激活就会触发这个方法
if(textField.frame.origin.y > HEIGHT / 2){
//先做个差
CGFloat height = textField.frame.origin.y - HEIGHT/2
self.view.center = CGPointMake(self.view.center.x,self.view.center.y - height);
}
return YES;
}
//等编译结束的时候再让它回到原位置 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//整个是在移动self.view,父视图的移动会让所有的子视图一同移动,而且相对父视图的坐标位置不会发生变化,所以可以沿用之前的判断
if(textfield.frame.origin.y > HEIGHT/2){
//先做一个差
CGFloat height = textField.frame.origin.y - HEIGHT/2;
self.view.center= CGPointMake(self.view.center.x,self.view.center.y + height);
}
return YES
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// button 点击改变背景随机色
-(void)click:(UIButton * )button
{
//随机颜色
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}