我对UIViewController中的几件事感到非常困惑,我已经阅读了View Controller Programming Guide并在Internet上进行了大量搜索,但仍然感到困惑。

当我想从firstVC跳转或切换到secondVC时,有多少种方法可用?我列出了我所知道的:

  • UINavigationController
  • UITabBarController
  • presentModalViewController:
  • 将secondVC添加到根视图
  • 如果将secondVC添加到根视图,那么将如何释放firstVC对象?
  • 是否添加每个我想跳转/切换到根视图的视图是一个好习惯?
  • transitionFromView:
  • 我不了解Apple Doc的这部分内容:

  • 此方法仅修改其视图层次结构中的视图。确实
    请勿以任何方式修改应用程序的视图控制器。对于
    例如,如果您使用此方法来更改由
    视图控制器,您有责任更新视图
    控制器适当地处理更改。

    如果我这样做:
    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点中所述的方法。动态更改根视图控制器的方法不是一个好习惯。如果要遵循Apple标准,则不应在不更改window根VC的情况下在applicationdidfinishlaunchingwithoptions上定义该窗口。

    转换示例FromView: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 */
                      }];
      }
    
    }
    

    10-08 03:32