我对于UIViewController中的几件事情感到很困惑

问题描述:

我对UIViewController中的几件事情感到非常困惑,我已经阅读了View Controller Programming Guide,并在互联网上搜索了很多,但仍然感到困惑。我对于UIViewController中的几件事情感到很困惑

当我想跳转或从firstVC切换到​​有多少类型的方法可用?我列出我知道:

  1. UINavigationController

  2. UITabBarController

  3. presentModalViewController:

  4. 添加secondVC到根视图

    • 如果secondVC被添加到根视图那么firstVC对象将如何被释放?
    • 添加想要跳转/切换到根视图的每个视图是否是一种很好的做法?
  5. transitionFromView:

    • 我不明白苹果文档此部分:

此法修改的意见,只是他们的视图层次。它不会以任何方式修改您的应用程序的视图控制器。例如,对于 示例,如果您使用此方法更改由视图控制器显示的根视图,则您有责任适当更新视图控制器以处理更改。

如果我这样做:

secondViewController *sVc = [[secondViewController alloc]init]; 

[transitionFromView:self.view toView:sVc.view... 

不过viewDidLoad:viewWillAppear:viewDidAppear:都做工精细:我不需要给他们打电话。那么苹果为什么这样说:

它是你的责任,适当地更新视图控制器来处理更改。

是否有其他方法可用?

实际使用的标准方法是:

1)使用NavigationController

//push the another VC to the stack 
[self.navigationController pushViewController:anotherVC animated:YES]; 

//remove it from the stack 
[self.navigationController popViewControllerAnimated:NO]; 

//or presenting another VC from current navigationController  
[self.navigationController presentViewController:anotherVC animated:YES completion:nil]; 

//dismiss it 
[self.navigationController dismissViewControllerAnimated:YES completion:nil]; 

2)呈现VC

//presenting another VC from current VC  
[self presentViewController:anotherVC animated:YES completion:nil 

//dismiss it 
[self dismissViewControllerAnimated:YES completion:nil]; 

不要使用您在点4所描述的方法,这不是一个很好的实践来动态改变根视图控制器。窗口的根VC通常在applicationdidfinish之后定义,然后在选择后选择它,如果你要遵循apple标准,就不应该改变它。对于transitionFromView

实施例:toView

-(IBAction) anAction:(id) sender { 
// assume view1 and view2 are some subviews of self.view 
// view1 will be replaced with view2 in the view hierarchy 
[UIView transitionFromView:view1 
        toView:view2 
        duration:0.5 
        options:UIViewAnimationOptionTransitionFlipFromLeft 
       completion:^(BOOL finished){ 
        /* do something on animation completion */ 
        }]; 
    } 

}