在IOS Swift应用程序中收到推送通知后,我想根据通知中的内容做两件事:
第二个我认为是第一个的先决条件。
我知道我需要在这两个函数中放置代码:
错误消息:'UIViewcontroller?'没有名为'navigationController'的成员
在AppDelegate.swift文件中:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
println("didReceiveRemoteNotification")
//Navigate to rootviewcontroller
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows")
as ViewController
//rootViewController.navigationController?
// .popToViewController(setViewController, animated: false)
}
在您的方法中尝试此代码:
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard
.instantiateViewControllerWithIdentifier("CurrentShows")
as ViewController_CurrentShows
rootViewController
.navigationController
.popToViewController(setViewController, animated: false)
需要创建一个变量:var window:UIWindow
下面的代码在appdelegate中实现,在:func didBecomeActive()....
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard.instantiateViewControllerWithIdentifier("Login") as Login_ViewController
self.window?.rootViewController?.dismissViewControllerAnimated(false, completion:
{
if self.window != nil
{
self.window!.rootViewController = rootController
}
})
我编写了一个简单的类,通过传递类类型,从一行代码中的任何位置导航到视图层次结构中的任何视图控制器,因此您将编写的代码也将与视图层次结构本身分离,例如:
Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()
..或者你也可以在主线程上异步执行方法:
Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
MyViewControllerInstance?.doSomethingAsync()
}
这里是GitHub项目链接:https://github.com/oblq/Navigator