使用swift从app delegate打开视图控制器 [英] Opening view controller from app delegate using swift
问题描述
我正在尝试创建推送通知,根据从推送中获得的信息确定要打开的视图。
I am trying to create a push notification which determines which view to open according to information obtained from the push.
我已设法从推送中获取信息,但我现在正努力让视图开启
I have managed to get the information from the push, but I am now struggling to get the view to open
查看其他堆栈溢出问题我目前有以下内容:
Looking at other stack overflow questions I have the following currently:
App Delegate完成加载:
App Delegate Did finish loading:
//Extract the notification data
if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
// Get which page to open
let viewload = notificationPayload["view"] as? NSString
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
//Load correct view
if viewload == "circles" {
var viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("Circles") as! UIViewController
self.window?.rootViewController = viewController
}
}
目前这在var ViewController = self ... line上失败。
Currently this is failing on the var ViewController = self... line.
推荐答案
你必须设置ViewController StoryBoardId属性如下图所示。
You have to set ViewController StoryBoardId property as below image.
在swift中使用以下编码打开viewController
open viewController using coding as below in swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
return true
}
这篇关于使用swift从app delegate打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!