简体   繁体   English

iOS presentViewController不会调用viewDidLoad

[英]iOS presentViewController doesn't invoke viewDidLoad

I'm implementing my own 'back' button. 我正在实现自己的“后退”按钮。 Where onClick , the following code is executed in the ViewController (VC) being dismissed: onClick ,以下代码在被解除的ViewController (VC)中执行:

  1. Dismiss current VC (VC#1) 关闭当前VC(VC#1)
  2. Pop current VC (VC#1) off my custom navigationStack 弹出当前VC(VC#1)关闭我的自定义navigationStack
  3. Get the last VC (VC#2) from the navigationStack , and present it using navigationStack获取最后一个VC(VC#2),然后使用它

presentViewController presentViewController

What happens is the back works visually works - ie current VC disappears, previous VC appears. 后面的工作是视觉效果 - 即当前VC消失,之前的VC出现。 However, the viewDidLoad method is not called. 但是,不会调用viewDidLoad方法。 So the screen isn't updated with data updates from viewDidLoad . 因此,不会使用viewDidLoad数据更新来更新屏幕。

 [self dismissCurrentViewController:self completion:^{
    [TWStatus dismiss];
    FHBaseViewController *vcToDisplay = [[FHDataManager sharedInstance] popNavigationStack];
    [vcToDisplay.homeVC presentViewController:vcToDisplay animated:NO completion: ^{ }];
}];

Questions: 问题:

  1. I was under the impression that viewDidLoad always gets called when presentViuewController is used?? 我的印象是当使用presentViuewController时, viewDidLoad总是被调用?
  2. I 'build' the screen using a method called ONLY from viewDidLoad in VC#2. 我使用VC#2中viewDidLoad中名为ONLY的方法“构建”屏幕。 How is iOS displaying the screen without coming into viewDidLoad ? iOS如何在不进入viewDidLoad情况下显示屏幕?

btw, I'm not using storyboards. 顺便说一下,我没有使用故事板。 Any help is appreciated! 任何帮助表示赞赏!

My guess is that viewWillAppear is being called but viewDidLoad is not, at least not when you expect it is. 我的猜测是调用viewWillAppear但是viewDidLoad不是,至少不是你预期的那样。 viewDidLoad should be called once, but depending on how you're managing the view controllers, viewDidLoad may not be triggered every time your view appears (which happens after loading). viewDidLoad应该被调用一次,但是根据你管理视图控制器的方式,每次你的视图出现时都可能不会触发viewDidLoad (加载后会发生这种情况)。

The completion handler is called after the viewDidAppear: method is called on the presented view controller. from presentViewController doc 来自presentViewController doc

so put this in your code with a breakpoint on the call to super and verify it is getting called when this transition occurs. 所以把这个放在你的代码中,在调用super时使用断点,并在发生此转换时验证它是否被调用。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

edit : since you verified that viewWillAppear is getting called, then I would say that it's coming down to how you are managing the view controller life cycle. 编辑 :既然你已经验证了viewWillAppear被调用了,那么我会说它是关于如何管理视图控制器生命周期的。 Even with a standard UINavigationController , viewDidLoad is not called when a view is shown as a result of popping items on the navigation stack. 即使使用标准的UINavigationController ,当视图显示为导航堆栈上弹出项目的结果时,也不会调用viewDidLoad I would move your logic to viewWillAppear if you are dead set on not using UINavigationController 如果您没有使用UINavigationController我会将您的逻辑移动到viewWillAppear

When I make a back button pragmatically I use: 当我务实地制作后退按钮时,我使用:

[self.navigationController popViewControllerAnimated:YES];

This will invoke the viewDidLoad method. 这将调用viewDidLoad方法。 Use that instead of your current code. 使用它而不是您当前的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 presentViewController不存在 - presentViewController doesn't present UIScrollView不响应scrollRectToVisible:animated:从iOS 5中的ViewDidLoad调用 - UIScrollView doesn't respond to scrollRectToVisible:animated: called from ViewDidLoad in iOS 5 尽管调用了viewDidLoad,但未出现iOS ViewController - iOS ViewController doesn't appears although viewDidLoad gets called 如何使用presentViewController转到ViewController并在iOS中执行或调用其中的方法 - How to use presentViewController to go to a viewcontroller and perform or invoke a method in it in ios 为什么presentViewController 再次调用viewDidLoad? - Why presentViewController calls viewDidLoad again? 来自超类的 Swift presentViewController 不起作用 - Swift presentViewController from superclass doesn't work presentViewController透明背景在iPad 4上不起作用 - presentViewController transparent background doesn't work on iPad 4 presentViewController不显示视图,但是调用了viewDidLoad(…) - presentViewController not presenting view, but viewDidLoad(…) does get called UIButton框架未按预期在viewDidLoad中进行布局 - UIButton frame doesn't layout as expected in viewDidLoad 为什么不在viewDidLoad中执行SegueWithIdentifier工作? - Why doesn't performSegueWithIdentifier work inside viewDidLoad?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM