gpt4 book ai didi

ios - 我真的对 UIViewController 中的一些事情感到困惑

转载 作者:行者123 更新时间:2023-11-29 13:13:55 26 4
gpt4 key购买 nike

我对 UIViewController 中的一些事情感到很困惑,我已经阅读了 View Controller Programming Guide在网上查了很多还是一头雾水。

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

  1. UINavigationController

  2. UITabBarController

  3. presentModalViewController:

  4. 将第二个VC添加到 Root View

    • 如果将 secondVC 添加到 Root View ,那么将如何释放 firstVC 对象?
    • 将我想要跳转/切换到 Root View 的每个 View 添加到 Root View 是一种好习惯吗?
  5. transitionFromView:

    • 我不明白 Apple 文档中的这一部分:

This method modifies the views in their view hierarchy only. It does not modify your application’s view controllers in any way. For example, if you use this method to change the root view displayed by a view controller, it is your responsibility to update the view controller appropriately to handle the change.

如果我这样做:

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

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

仍然 viewDidLoad:viewWillAppear:viewDidAppear: 工作正常:我不需要调用它们。那么为什么 Apple 会这样说:

it is your responsibility to update the view controller appropriately to handle the change.

还有其他方法吗?

最佳答案

实际上使用的标准方法是:

1) 使用导航 Controller

 //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 点中描述的方法。动态更改 Root View Controller 不是一个好习惯。窗口的根 VC 通常在 applicationdidfinishlaunchingwithoptions 上定义,如果您要遵循苹果标准,则不应更改。

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 */
}];
}

}

关于ios - 我真的对 UIViewController 中的一些事情感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com