2017-02-27 66 views
1

我有TabBarController幾個選項卡。 在每個選項卡中都有帶ViewController的NavigationController,它具有TableView和單元格。當用戶按下單元格時 - 他得到DetailedViewController。在TabBarController中打開ViewController處理推送通知

我想要的是 - 當我的應用程序收到推送通知時打開DetailedViewController。

我用OneSignal和我的AppDelegate didFinishLaunchingWithOptions樣子(你看,我有ITEMID,我想傳遞給DetailedViewController

OneSignal.initWithLaunchOptions(
      launchOptions, 
      appId: API_KEY, 
      handleNotificationAction : { 
       (result) in 
        if additionalData!["itemID"] != nil { 
         let itemID = Int(additionalData!["itemID"] as! String) 
         if(itemID! > 0) { 

         if(self.window?.rootViewController?.presentedViewController != nil) { 
          self.window?.rootViewController?.presentedViewController?.dismiss(animated: true, completion: { 
           let storyboard = UIStoryboard(name: "Main", bundle: nil) 
           let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController 
           tabBarController.selectedIndex = 1 
           self.window?.rootViewController?.present(tabBarController, animated: true, completion: nil) 
          }) 
         } 

        } 
       } 
       } 
     } 
    ) 
  1. 此代碼是不工作的一些原因 - 我沒有得到想要的 選項卡需要ViewController
  2. 我不知道如何從TabBarController打開DetailedViewController右邊
  3. 當我fina lly會進入DetailedViewController - 當我按下返回按鈕時會發生什麼?有沒有項目列表的ViewController - 我將在那裏導航?
  4. 是代碼要使用內存嗎?
+0

是否有解散'rootViewController'的理由?爲什麼不簡單地提供你想要的標籤控制器呢?這樣,至少你的後退按鈕將工作(一旦你得到的一切工作) –

+0

因爲當應用程序正在運行,並收到通知 - 如果我不會駁回提出的ViewController會發生什麼?它會打開新的,吃更多的記憶 - 我錯了嗎? – moonvader

+0

只需將您的rootViewController更改爲您所需的視圖控制器 – Mannopson

回答

1

試試這個:首先實例化TabBarController

let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController 

,改變你的rootViewController

self.window = UIWindow.init(frame: UIScreen.main.bounds) 
    self.window?.rootViewController = tabBarController 
    self.window?.makeKeyAndVisible() 

還可以自定義一個全局tintColor就在這裏:

self.window?.tintColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0) 

最後設置你選擇的指數:

tabBarController.selectedIndex = 1 

就是這樣:它應該工作。讓我知道它是否有效!

let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController 
    tabBarController.selectedIndex = 1 

    self.window = UIWindow.init(frame: UIScreen.main.bounds) 
    self.window?.tintColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0) 
    self.window?.rootViewController = tabBarController 
    self.window?.makeKeyAndVisible()