ios 不鼓励在分离的视图控制器上展示视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25144400/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:42:58  来源:igfitidea点击:

Presenting view controllers on detached view controllers is discourage

iosxcodeamslidemenu

提问by Alex Egorkin

I'm using xcode 5.1.1 with storyboard. I have a button on main menu and it pops to another view controller with this code

我在故事板中使用 xcode 5.1.1。我在主菜单上有一个按钮,它使用此代码弹出到另一个视图控制器

VC *secondVC = [[VC alloc] init];
[self presentViewController:secondVC animated:YES completion: nil];

And there I have back button with this code

在那里我有带有此代码的后退按钮

[self dismissViewControllerAnimated:YES completion: nil];

And when I pop to secondVC xcode gives me is error:

当我弹出到 secondVC xcode 给我的是错误:

Presenting view controllers on detached view controllers is discourage <UINavigationController: 0x8c94510>.

不鼓励在分离的视图控制器上呈现视图控制器<UINavigationController: 0x8c94510>

I'm also having problem with rotation, it doesn't work properly.

我也有旋转问题,它不能正常工作。

回答by Eric Alford

This warning will come up if if you are trying to push, present or pop view controllers in viewWillAppear. I don't know what is in your code so it's tough to identify your problem but try using this if you are trying to show new views in viewWillAppear.

如果您尝试在 viewWillAppear 中推送、呈现或弹出视图控制器,则会出现此警告。我不知道您的代码中有什么内容,因此很难确定您的问题,但是如果您尝试在 viewWillAppear 中显示新视图,请尝试使用它。

[self performSelector:@selector(yourMethod)
               withObject:nil afterDelay:0.0];

回答by Aaron A.

Present the controller from viewDidAppear:instead.

viewDidAppear:而是显示控制器。

回答by XplohrMohr

I ran into a similar situation while attempting to present an UIAlertController from viewWillAppear. Since performSelector does not exist in Swift, I wrapped my call this way:

我在尝试从 viewWillAppear 呈现 UIAlertController 时遇到了类似的情况。由于 Swift 中不存在 performSelector,因此我以这种方式包装了我的调用:

dispatch_async(dispatch_get_main_queue()), {
    presentYourVCHere;
});

回答by Fede Cugliandolo

Try pushing from rootVC:

尝试从 rootVC 推送:

[self.view.window.rootViewController.navigationController pushViewController:YOUR_VIEW_CONTROLER animated:YES];

回答by UIResponder

try calling the same action in viewDidAppear. this warning is due to when we present any view in viewDidLoad or viewWillAppear method.

尝试在 viewDidAppear 中调用相同的操作。这个警告是由于我们在 viewDidLoad 或 viewWillAppear 方法中呈现任何视图。

so do this in override func viewDidAppear(animated: Bool) { ... }

所以在覆盖 func viewDidAppear(animated: Bool) { ... }

回答by skantner

I was getting the same error when presenting a UIAlertController from a UIViewController (vc2) nested inside another UIViewController (vc1)

从嵌套在另一个 UIViewController (vc1) 中的 UIViewController (vc2) 呈现 UIAlertController 时,我遇到了同样的错误

I solved it like so:

我是这样解决的:

[vc1 addChildViewController:vc2]

After doing that I could present UIAlertController from vc2 without any error messages.

这样做之后,我可以从 vc2 显示 UIAlertController 而没有任何错误消息。

回答by Yamir Abdiel Ortega

in case this error happens from popup view controller you need to call the code for show the alert controller directly from parent view using delegation. For example in your parent view controller:

如果弹出视图控制器发生此错误,您需要使用委托直接从父视图调用代码以显示警报控制器。例如在您的父视图控制器中:

-(void) showAlertError: (YourClass *) whateverParameter {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Your Title"
                                                                       message:@"Your message"
                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    });
}

And in your child view:

在您的孩子看来:

- (IBAction)btnDone:(id)sender{
    [self.delegate showAlertError:self];
}

回答by Augustine P A

None of the answers helped me. And here the code which I used to resolve the issue.

没有一个答案对我有帮助。这里是我用来解决问题的代码。

let topViewController = (self.navigationController?.viewControllers.last)! as UIViewController
topViewController.presentViewController(secondView, animated: true, completion: nil)

Hope this will help someone :)

希望这会帮助某人:)

回答by Chinthaka

If anyone wonder how you could do this using swift (even can be used in Objective C), following code help me to overcome the above issue. Use this code inside the viewDidLoad()

如果有人想知道如何使用 swift 执行此操作(甚至可以在 Objective C 中使用),以下代码可帮助我克服上述问题。在 viewDidLoad() 中使用此代码

var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), { () -> Void in
         //this place to call segue or manually load the view. 
})

回答by Ivan Cantarino

I was trying to present the UIViewControllerfrom a splitViewController.

我试图呈现UIViewController来自splitViewController.

This is what solved my issue:

这就是解决我的问题的原因:

self.view.window.rootViewController.present(yourViewController, animated: true, completion: nil)