iOS - ViewController基础

类似于android的activity。

生命周期函数

  • (void)viewDidLoad //视图加载,只执行一次
    -(void) viewWillAppear:(BOOL)animated // 视图即将显示,每次即将显示时,执行一次
    -(void) viewDidAppear:(BOOL)animated // 视图显示,每显示出来一次,执行一次
    -(void) viewWillDisappear:(BOOL)animated // 视图即将消失,每次即将消失时,执行一次
    -(void) viewDidDisappear:(BOOL)animated // 视图消失,每消失一次,执行一次
  • (void)didReceiveMemoryWarning//内存过低时,警告

切换ViewController

显示新的视图:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    VC1* vc = [[VC1 alloc]init]; //VC1为类名
    [self presentViewController:vc animated:true completion:nil];
}

隐藏当前视图

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:true completion:nil];
}

你可能感兴趣的:(iOS - ViewController基础)