ios viewDidAppear 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11532808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
viewDidAppear not getting called
提问by xonegirlz
In my main UIViewController I am adding a homescreen view controller as subviews:
在我的主 UIViewController 中,我添加了一个主屏幕视图控制器作为子视图:
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vc];
controller.navigationBarHidden = YES;
controller.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:controller];
[self.view insertSubview:controller.view atIndex:0];
[controller didMoveToParentViewController:self];
The issue is that viewDidAppear and viewWillAppear is only called once, just like viewDidLoad. Why is this? How do I make this work?
问题是 viewDidAppear 和 viewWillAppear 只被调用一次,就像 viewDidLoad 一样。为什么是这样?我如何使这项工作?
Basically inside vc I am not getting viewDidAppear nor viewWillAppear.
基本上在 vc 中,我没有得到 viewDidAppear 或 viewWillAppear。
I also just tried adding the UIViewController without the navigation controller and it still doesn't work:
我也只是尝试在没有导航控制器的情况下添加 UIViewController ,但它仍然不起作用:
vc.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:vc];
[self.view insertSubview:vc.view atIndex:0];
[vc didMoveToParentViewController:self];
采纳答案by Alex Gosselin
Presenting view controllers using presentModalViewController or segues or pushViewController should fix it.
使用 presentModalViewController 或 segues 或 pushViewController 呈现视图控制器应该修复它。
Alternatively, if for some reason you want to present your views without the built-in methods, in your own code you should be calling these methods manually. Something like this:
或者,如果出于某种原因您想在没有内置方法的情况下显示您的视图,则您应该在自己的代码中手动调用这些方法。像这样的东西:
[self addChildViewController:controller];
BOOL animated = NO;
[controller viewWillAppear:animated];
[self.view insertSubview:controller.view atIndex:0];
[controller viewDidAppear:animated];
[controller didMoveToParentViewController:self];
回答by luky
In my case, viewDidAppear was not called, because i have made unwanted mistake in viewWillAppear method.
就我而言,没有调用 viewDidAppear,因为我在 viewWillAppear 方法中犯了不必要的错误。
-(void)viewWillAppear:(BOOL)animated {
[super viewdidAppear:animated]; // this prevented my viewDidAppear method to be called
}
回答by Rob
The only way I can reproduce the problem of child controllers not receiving appearance methods is if the container view controller does the following (which I'm sure you're not doing):
我可以重现子控制器不接收外观方法的问题的唯一方法是容器视图控制器是否执行以下操作(我确定您没有这样做):
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
{
return NO;
}
Perhaps you can try explicitly enabling this:
也许您可以尝试显式启用此功能:
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
{
return YES;
}
But my child view controllers definitely are getting the viewWillAppear
calls (either if I explicitly automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
or if I omit this altogether.
但是我的子视图控制器肯定会收到viewWillAppear
调用(如果我明确地automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
或者我完全忽略了这一点。
Update:
更新:
Ok, looking at the comments under your original question, it appears that the issue is that the child controller (B) in question is, itself, a container view controller (which is perfectly acceptable) presenting another child controller (C). And this controller B's own child controller C is being removed and you're wondering why you're not getting viewWillAppear
or viewDidAppear
for the container controller B. Container controllers do notget these appearance methods when their children are removed (or, more accurately, since containers should remove children, not children removing themselves, when the container removes a child, it does not receive the appearance methods).
好的,查看原始问题下的评论,似乎问题在于所讨论的子控制器(B)本身是一个容器视图控制器(完全可以接受),呈现另一个子控制器(C)。并且这个控制器 B 自己的子控制器 C 正在被移除,你想知道为什么你没有得到viewWillAppear
或viewDidAppear
为容器控制器 B.当他们的孩子被移除时容器控制器没有获得这些外观方法(或者,更准确地说,因为容器应该取出儿童,而不是儿童自行取出,容器取出儿童时,不接收外观方法)。
If I've misunderstood the situation, let me know.
如果我误解了情况,请告诉我。
回答by Ivan Cantarino
@Rob answer in Swift 4 (which helped me on my case which I was adding a childViewController
to a UITabBarController
)
@Rob 在 Swift 4 中回答(这帮助我解决了我在 a 中添加 achildViewController
的情况UITabBarController
)
override var shouldAutomaticallyForwardAppearanceMethods: Bool {
return true
}
回答by Tim
Had a same problem
My container view controller did retain
a child view controller via a property
, butdid not add a child view controller to its childViewControllers
array
.
有同样的问题我的容器视图控制器retain
通过 a做了一个子视图控制器property
,但没有在它的childViewControllers
array
.
My solution was to add this line of code in the container view controller
我的解决方案是在容器视图控制器中添加这行代码
[self addChildViewController: childViewController];
After that UIKit
started forwarding appearance methods to my child view controller just as expected
之后UIKit
开始按预期将外观方法转发给我的子视图控制器
I also changed the property attribute from strong
to weak
just for beauty
我还将属性属性从 更改strong
为weak
只是为了美观
回答by RichApple
When updating my code to 13.0, I lost my viewDidAppear calls.
将我的代码更新到 13.0 时,我丢失了 viewDidAppear 调用。
In Objective-c, my solution was to add the following override all to the parent master view controller.
在 Objective-c 中,我的解决方案是将以下覆盖全部添加到父主视图控制器。
This allowed the ViewDidAppear call to get called once again...as it did in previous IOS (12 and earlier) version.
这允许再次调用 ViewDidAppear 调用......就像它在以前的 IOS(12 及更早版本)版本中所做的那样。
@implementation MasterViewController
@implementation 主视图控制器
//....some methods
//....一些方法
- (BOOL) shouldAutomaticallyForwardAppearanceMethods { return YES; }
- (BOOL) shouldAutomaticallyForwardAppearanceMethods { return YES; }
// ...some methods
// ...一些方法
@end
@结尾
回答by user2905353
Another case where this will not be called at launch time (yet may be called on when you return to the view) will be is if you have subclassed UINavigationController
and your subclass overrides
另一个不会在启动时调用(但在返回视图时可能会调用)的情况是,如果您已创建子类UINavigationController
并且您的子类覆盖
-(void)viewDidAppear:(BOOL)animated
-(void)viewDidAppear:(BOOL)animated
but fails to call [super viewDidAppear:animated];
但无法调用 [super viewDidAppear:animated];
回答by Marián ?erny
My problem was that I was changing the tab in UITabBarController (selectedIndex = x) and then messing with the child view controllers in that tab. The problem is that it needs to be done the other way round: first mess with the child view controllers in other tab and then change the tab by setting the selectedIndex. After this change methods viewWillAppear/viewDidAppear begun to be called correctly.
我的问题是我正在更改 UITabBarController (selectedIndex = x) 中的选项卡,然后弄乱了该选项卡中的子视图控制器。问题是它需要以相反的方式完成:首先弄乱其他选项卡中的子视图控制器,然后通过设置 selectedIndex 来更改选项卡。在此更改方法 viewWillAppear/viewDidAppear 之后开始被正确调用。