我试图在我的函数中使用它来在函数运行后呈现视图控制器,因此将其包含在
中let window = UIWindow()
let rootViewController = window.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("EmployeeTBCIdentifier")
rootViewController?.presentViewController(setViewController, animated: true, completion: nil)
它没有做任何事情,如果我在viewDidLoad()
中使用它,它会起作用这是主要的想法,并且可以根据这个例子使用完成,
func logInType(completion(Void1, Void2)->()) {
if login == employer{
void1
}else if login == employee{
void2
}
}
使用我想要返回的功能时
func logInType{(void1, void2) -> void in
void1 = self.presentview //I want to push view to another if this code executed
void2 = self.presentview //same idea
}
已更新:代码正在运行,但它没有推送到已识别的viewcontroller。
dispatch_async(dispatch_get_main_queue()){
let window = UIWindow()
let rootViewController = window.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("EmployeeTBCIdentifier")
rootViewController?.presentViewController(setViewController, animated: true, completion: nil)
}
答案 0 :(得分:1)
在我看来,如何获得rootViewController
可能存在问题。我认为let window = UIWindow()
调用将返回一个新的UIWindow
对象而不是你想要的对象,这是根视图控制器(和你的应用程序)关联的窗口。继续此操作,rootViewController
将为nil
,您的可选链调用rootViewController?.presentViewController(setViewController, animated: true, completion: nil)
将不会执行任何操作。通过rootViewController!.presentViewController(setViewController, animated: true, completion: nil)
使用显式展开,您应该会看到错误
要纠正,请使用UIAppDelegate
从OtherViewController.view!.window!
或其他视图控制器以另一种方式获取正确的UIWindow
答案 1 :(得分:0)
尝试使用它:
dispatch_async(dispatch_get_main_queue())
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
self.presentViewController(vc, animated: true, completion: nil)
}