我是 Swift Mac 应用程序开发的新手,在登录 URLRequest 后从登录窗口转到显示主窗口,并在新的主窗口中创建另一个 URLRequest 时遇到麻烦。如果我只是从一个窗口进入而不进行登录 URLRequest,它就可以正常工作。
func loadMainView() {
self.view.window?.close()
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let mainWindowController = storyboard.instantiateController(withIdentifier: "MainViewController") as! NSWindowController
if let mainWindow = mainWindowController.window {
let application1 = NSApplication.shared()
application1.runModal(for: mainWindow)
}
}
func tryLogin(_ username: String, password: String ) {
Staff.login(username: self.username.stringValue, password: self.password.stringValue) { (completed, result, staff) in
// DispatchQueue.main.async {
if completed {
if result == .Success && staff != nil {
DispatchQueue.main.async(execute: {
self.loadMainView()
})
} else {
self.dialogOKCancel(message: "Error", text: "Your credentials are incorrect")
}
} else {
print("error")
}
}
}
HTTPSConnection.httpGetRequestURL(token: token, url: digialOceanURL, mainKey: mainKeyVal) { ( complete, results) in
DispatchQueue.main.async {
if complete {
}
}
}
我尝试在不执行的情况下调用 self.loadMainView(),但仍然不走运。
任何帮助表示赞赏。谢谢
最佳答案
不要在模态中运行主视图,模态应该用于对话框。因此,您可以在模式中运行登录 View (并通过在应用程序上调用 stopModal
来完成它)。在这种情况下,您可以使用像 loadLoginView
这样的东西,它将与您当前的 loadMainView
具有类似的实现(但没有这个 self.view.window?.close()
调用。主视图将在应用程序启动时从 nib 加载。但是您必须发布更多代码(您的应用程序初始化是什么样子?)才能获得相关帮助。
关于swift - 进行异步调用后运行 Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204429/