Ios 通过带有storyBoardID的硬代码加载ViewController

Ios 通过带有storyBoardID的硬代码加载ViewController,ios,swift,viewcontroller,Ios,Swift,Viewcontroller,我正试图通过以下代码加载视图: let storyboard = UIStoryboard(name: "Main", bundle: nil) //let vc = self let vc = storyboard.instantiateViewController(withIdentifier: "loginView") as! RegistrationViewController self.present(vc, animated: true, completion: nil) 结果是:

我正试图通过以下代码加载视图:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let vc = self
let vc = storyboard.instantiateViewController(withIdentifier: "loginView") as! RegistrationViewController
self.present(vc, animated: true, completion: nil)
结果是:

2017-07-24 16:39:48.135 XXX [8689:286531] Warning: Attempt to present <XXX.RegistrationViewController: 0x7f8246206280> on <XXX.LaunchAppCheck: 0x7f8243d06bd0> whose view is not in the window hierarchy!
2017-07-24 16:39:48.135 XXX[8689:286531]警告:试图在不在窗口层次结构中的视图上显示!

我已经在另一个问题中看到了尝试使用
viewdideappear()
,但问题仍然存在。

可能这一行让您感到困扰:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
这是实例化除实际故事板之外的另一个故事板

这是我使用的代码:

let vc: SettingsViewController = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
self.present(vc, animated: true, completion: nil)
您不应尝试将其与另一个viewController(即viewDidLoad())同时加载,因为此时它还不在堆栈上

如果确实需要同时显示2个ViewController,请尝试使用延迟调用第二个:

vc = self.storyboard!.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
         self.present(vc, animated: true, completion: nil)
    }
也许有帮助

不要忘记在Identity Inspector中设置视图的id


这是警告还是撞车?警告:没有撞车。应用程序仍然保留在第一个viewController上,因为它无法加载所需的viewController,如我的代码中所示。它告诉我,在初始化之前,常量“vc”已在闭包中捕获。我认为在我的应用程序中“按原样”运行时,您的代码中有一些错误您忘记了初始化“vc”变量:正如我提到的,它不是初始化的,只是声明的。要正常工作,它必须按如下方式初始化:vc=storyboard.instantialeviewcontroller(带有标识符:“setingsviewcontroller”)为!设置可视控制器。在调用Dispatcher之前添加这一行,我会将您的答案标记为“已接受!”:)啊。我理解您的意思。我给了您两种方法。声明和实例在第一种方法上。第二种方法我刚刚使用Dispatche进行了调用。我将编辑它