iOS开发之ViewControler

关于视图控制器
**视图控制器用来控制 view 和 model 之间的交互,在iOS中,用UIViewControler表示视图控制器
此类中有一些特定的方法需要开发者在开发过程中完成
1.初始化方法
2.加载视图的方法**

初始化方法

//下面两个初始化方法,可以重写父类方法,实现自己需要的功能
- (id)init {
    if(self = [super init]){

    }    
    return  self;
}
//带nib文件的初始化方法
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){


    }
    return  self;
}

视图的生命周期
在视图的不同生命周期中,视图控制器会回调不同的方法
一个控制器必须带有一个根视图

//加载视图
- (void)loadView{
    //调用super让父类去帮我们完成一些初始化工作,override重写父类的方法
    [super loadView];

    NSLog(@"加载视图");
}

//在视图控制器已被实例化,还会被加载到内存中的时候,会调用viewDidLoad,这时视图并没有出现。该方法只调用一次。
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    //打印方法名
    NSLog(@"%s",__func__);
}
//以上两种方法在视图控制器类被实例化后,只执行一次。

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    NSLog(@"视图将要出现");
}
- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    NSLog(@"视图已经出现");
}
- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    NSLog(@"视图将要消失");

}
- (void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];
    NSLog(@"视图已经消失");
}

创建一个button,添加到buttonAction方法

- (IBAction)buttonAction:(UIButton *)sender { 
    NSLog(@"点我");   
    //初始化一个视图控制器
    BViewController *bVC = [[BViewController alloc] initWithNibName:@"BViewController" bundle:[NSBundle mainBundle]];
    //从本视图推送到另一个视图,即bVC控制器所在视图
    [self presentViewController:bVC animated:YES completion:^{
        //完成后实现的方法
        NSLog(@"模态推送完成");
    }];
}
//返回到推送之前的视图,到BViewController类里创建一个按钮并添加到方法
- (IBAction)backAction:(UIButton *)sender {

    NSLog(@"b点我");
    //返回到上一个视图
    [self dismissViewControllerAnimated:YES completion:^{
        //完成后实现的方法
    }];

与UIView有关的属性

- (void)viewDidLoad {
    [super viewDidLoad];

    flag = YES;
    self.view.backgroundColor = [UIColor orangeColor];
    //打印方法名
    NSLog(@"%s",__func__);

    //UIView
    //UIView为所有控件的父类
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
    //给视图添加背景颜色
    view.backgroundColor = [UIColor blueColor];
    //设置圆角
    view.layer.cornerRadius = 5.0;
    //其它层超出范围的部分隐藏起来
    view.layer.masksToBounds = YES;

    view.layer.borderWidth = 1.0;
    view.layer.borderColor = [UIColor yellowColor].CGColor;

    [self.view addSubview:view];

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(35, 35, 30, 30)];
    view1.backgroundColor = [UIColor greenColor];
    [view addSubview:view1];
    /*
     理解子视图和父视图的概念
     有且仅有一个父视图
     UIView *superView = view.superview;
     有多个子视图
     NSArray *array = self.view.subviews;

     每个视图都有添加子视图的方法:addSubview
     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(35, 35, 30, 30)];
     view1.backgroundColor = [UIColor greenColor];
     [view addSubview:view1];

     //关于视图的位置:UIView类给了三个属性:frame  bounds  center
     /*
     frame 和 bounds 的区别
     其中frame是参考父视图的左上角
     而bounds是参考它自己

     */

    NSLog(@"view.frame = %@",[NSValue valueWithCGRect:view.frame]);
    NSLog(@"view.bounds = %@",[NSValue valueWithCGRect:view.bounds]);
    NSLog(@"view.center = %@",[NSValue valueWithCGPoint:view.center]);

    //写一个button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 200, 100, 300);
    button.backgroundColor = [UIColor greenColor];
    [button setTitle:@"点击" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //添加点击事件
    [button addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    //每一个view都有一个tag属性
    view.tag = 1000;

    [self.view addSubview:button];
}

实现视图的移动大小变化及回到原来状态,即用UIView实现简单的动画

- (void)buttonAction2:(UIButton *)sender{
    //通过tag值获得viewDidLoad里的view,是同一个view
    UIView *view = [self.view viewWithTag:1000];
    //BOOL默认为NO,即flag默认为NO,所以在viewDidLoad设置flag值为YES
    if (flag) {
         //表示在2.0秒之内从原来位置移动到指定目标位置,并且逐渐变大    
        [UIView animateWithDuration:2.0 animations:^{
            view.frame = CGRectMake(200, 50, 150, 150);
        }];
    }
    else{
    //表示回到原来状态(位置和大小)
        [UIView animateWithDuration:2.0 animations:^{
            view.frame = CGRectMake(10, 50, 120, 120);
        }];        
    }
    //两个状态,可以想到用BOOL来进行判断
    flag = !flag;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值