iOS学习应用开发就业课_009:UIViewController的使用

本文介绍iOS应用中视图的三种状态:显示前、显示中和隐藏,并通过示例代码展示了如何从一个ViewController切换到另一个View02。同时记录了视图生命周期中的关键方法调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习:

1.//视图的3个状态:

a.显示前

b.正处于显示状态

c.已经被影藏


2.切换页面:ViewController和View02


复习次数:

5月26日

5月27日

5月28日

6月6日失败


代码:

//

//  ViewController.m

//  0606

//

//  Created by Encore on 16/6/6.

//  Copyright © 2016 trauson. All rights reserved.

//


#import "ViewController.h"

#import "View02.h"

@interface ViewController ()


@end


@implementation ViewController


//1

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"程序第一次加载视图时的调用");

    self.view.backgroundColor=[UIColor yellowColor];

}


//4视图分为:消失后

-(void)viewDidDisappear:(BOOL)animated

{

    NSLog(@"视图已经被影藏");

}

//2显示前

-(void)viewWillAppear:(BOOL)animated

{

    NSLog(@"视图即将显示");

}

//3

-(void)viewWillDisappear:(BOOL)animated

{

    NSLog(@"视图即将消失");

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    View02* myView=[[View02 alloc] init];

    

    [self presentViewController:myView animated:YES completion:nil];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

View02的页面:

#import "View02.h"

#import "ViewController.h"

@interface View02 ()


@end


@implementation View02


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor blackColor];

    NSLog(@"View02同步显示!");

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self dismissViewControllerAnimated:YES completion:nil];

}


//4视图分为:消失后

-(void)viewDidDisappear:(BOOL)animated

{

    NSLog(@"View02视图已经被影藏");

}

//2显示前

-(void)viewWillAppear:(BOOL)animated

{

    NSLog(@"View02视图即将显示");

}

//3

-(void)viewWillDisappear:(BOOL)animated

{

    NSLog(@"View02视图即将消失");

}

/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值