2

目前具體視圖控制器,我已經在我的AppDelegate實現這兩種方法導航從AppDelegate中方法

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool 

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool 

第一個將被調用,如果用戶打開我的應用程序與Spotlight的搜索結果,第二個被調用,如果我的應用程序從Apple Maps打開(因爲它是一個路由應用程序)。

我的問題是,什麼是最好的方式去APPDELEGATE(獨立於用戶在哪個視圖)的具體UIViewController

我問的原因是因爲此刻我試圖根據用戶的位置手動導航到它。例如,它們可能位於模塊化顯示的UIViewController中(然後需要將其解除),或者它們可能在UINavigationController中較深,其中應用程序需要呼叫popToRootViewController

這樣做,代碼變得毛茸茸的,似乎並不正確。這樣做似乎也不正確,要麼是因爲它非常脆弱。

+0

檢查這個http://stackoverflow.com/a/26757245/3535399 –

+0

@vivektakrani,不完全我在找什麼...這些都是viewDidFinishLaunchingWithOptions ...只發生一次。我在談論另外兩種委託方法(在我的文章中提到),如果用戶來自Apple Maps或Spotlight,那麼這兩種方法都會發生 - 在應用程序的整個生命週期中,這兩種方法都可以很快樂。 –

回答

0

我只是試圖找出如何實現你提到的第一種方法,發現從https://www.hackingwithswift.com/read/32/4/how-to-add-core-spotlight-to-index-your-app-content 他的代碼是一個有益的開端:

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool { 
    if userActivity.activityType == CSSearchableItemActionType { 
     if let uniqueIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String { 
      let splitViewController = self.window!.rootViewController as! UISplitViewController 
      let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController 

      if let masterVC = navigationController.topViewController as? MasterViewController { 
       masterVC.showTutorial(Int(uniqueIdentifier)!) 
      } 
     } 
    } 

    return true 
} 

我發現self.window .rootViewController作爲? yourRootViewControllerClass爲您的問題提供了一個很好的跳板。

我的代碼,這是非常基本的,看起來是這樣的:

// create a storyBoard item to instantiate a viewController from. If you have multiple storyboards, use the appropriate one. I just have one so it is "Main" 
let sb = UIStoryboard(name: "Main", bundle: nil) 

// get the navigation controller from the window and instantiate the viewcontroller I need. 
if let viewController = sb.instantiateViewControllerWithIdentifier("DetailViewController") as? ViewController, 
let nav = window?.rootViewController as? UINavigationController { 

    viewController.setupController(bookName)// setup the viewController however you need to. I have a method that I use (I grabbed the bookName variable from the userActivity) 
    nav.pushViewController(viewController, animated: false)//use the navigation controller to present this view. 

} 

這對我的作品,但我一定要給一個警告。我的應用程序只有一個有三個viewControllers的故事板(NavController-> tableViewController-> ViewController)。我不確定這種邏輯如何適用於更復雜的應用程序。

另一個很好的參考是:http://www.appcoda.com/core-spotlight-framework/

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool { 
    let viewController = (window?.rootViewController as! UINavigationController).viewControllers[0] as! ViewController 
viewController.restoreUserActivityState(userActivity) 

    return true 
} 

那的viewController有這個方法:

override func restoreUserActivityState(activity: NSUserActivity) { 
    if activity.activityType == CSSearchableItemActionType { 
     if let userInfo = activity.userInfo { 
      let selectedMovie = userInfo[CSSearchableItemActivityIdentifier] as! String 
      selectedMovieIndex = Int(selectedMovie.componentsSeparatedByString(".").last!) 
      performSegueWithIdentifier("idSegueShowMovieDetails", sender: self) 
     } 
    } 
}