xcode Swift 3 尝试呈现其视图不在窗口层次结构中

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

Swift 3 Attempt to present whose view is not in the window hierarchy

iosswiftxcode

提问by Moaz Khan

This question has been asked many times but even after trying most of the possible things I am still unable to find a solution that works for me. Here is the error message.

这个问题已经被问过很多次了,但即使在尝试了大部分可能的事情之后,我仍然无法找到适合我的解决方案。这是错误消息。

Warning: Attempt to present on <0x7f8f29e17590> whose view is not in the window hierarchy!

警告:尝试在 <0x7f8f29e17590> 上显示其视图不在窗口层次结构中!

Note: I am not using any navigation controller.

注意:我没有使用任何导航控制器。

I am just presenting a view controller modallyand I have a button on it for linkedIn sign up. But every time I click the linkedin button this error appears and I am unable to see the new linkedIn dialog although it works fine in other classes.

我只是以模态方式呈现一个视图控制器,并且上面有一个用于linkedIn 注册的按钮。但是每次我单击linkedin 按钮时都会出现这个错误,我无法看到新的linkedIn 对话框,尽管它在其他类中工作正常。

Most solutions recommend handling button click in viewDidAppear already tried that and it doesn't work.

大多数解决方案都建议处理 viewDidAppear 中的按钮点击已经尝试过,但它不起作用。

I am using this code for opening linkedIn signup form

我正在使用此代码打开linkedIn注册表单

linkedinHelper.authorizeSuccess({ [unowned self] (lsToken) -> Void in

        print("success lsToken: \(lsToken)")
        self.requestProfile()
        }, error: { [unowned self] (error) -> Void in

            print("Encounter error: \(error.localizedDescription)")
        }, cancel: { [unowned self] () -> Void in

            print("User Cancelled!")
    })

回答by Moaz Khan

I have resolved the issue the main problem is exactly what it says the view is not in the view hierarchy. In order to resolve this issue we need to set the root view controller to the current view controller using the appDelegate object. So that the view now comes in the view hierarchy and be able to present further views. Here is the code

我已经解决了这个问题,主要问题正是它所说的视图不在视图层次结构中。为了解决这个问题,我们需要使用 appDelegate 对象将根视图控制器设置为当前视图控制器。以便视图现在进入视图层次结构并能够呈现更多视图。这是代码

let initialViewController = UIStoryboard(name: "Main", bundle:nil).instantiateInitialViewController() as UIViewController
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
appDelegate.window?.rootViewController = initialViewController

Please read this for further information. https://stackoverflow.com/a/27608804/5123516

请阅读本文以获取更多信息。https://stackoverflow.com/a/27608804/5123516