0

ローカル通知が実行され、そのカスタムアクションが実行されるたびに、をTabBarControllerから開きたいとします。 、second()TabBarControllerから特定のViewControllerを表示

func second() { 


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController 
    let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController 

    tabBarController.present(navigationController, animated: true) { 

    } 

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

} 

そして、それがうまく機能しています:

func first() { 


    let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController 
    let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController 

    tabBarController.present(navigationController, animated: true) { 

    } 

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

} 

セカンド機能

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    switch response.actionIdentifier { 
    case "first": 
     DispatchQueue.main.async(execute: { 
      self.first() 
     }) 
    case "second": 
     DispatchQueue.main.async(execute: { 
      self.second() 
     }) 
    default: 
     break 
    } 


    completionHandler() 
} 

だから、それはfirst()機能だ:私はコードの次の行を使用していますしかし、私はそれを開くことができません二ViewController最初のものが提示され、第二通知が発射されている間:コンソールで:のViewControllerを提示する警告しようと...

答えて

1

使用この:

tabBarController.selectedIndex = 1 

どこすることができますviewcontrollersのいずれかであるtabBarController

+0

これは役に立たないと思われます。それは1を返します – Mannopson

+1

何も返す必要はありません。 selectedIndexを任意の数値に設定すると、タブバーはタブを選択したインデックスに変更します。 selectedIndex = 0に設定すると、最初のタブに切り替わります。 selectedIndex = 1、2番目のタブ。そうですね、 –

+0

助けてくれてありがとう。私はUITabBarControllerクラスに関する経験はありません。 – Mannopson

1

私は同様の問題に遭遇し、selectedIndexを変更しました私のために働かない。プロジェクトの要件に応じて、ViewControllerをインスタンス化し、Subviewとして追加し、Subviewに移動することができます。あなたが終わったら、必ずSubviewを取り除いてください。

let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView") 
    self.addChildViewController(replyView!) 
    self.view.addSubview(replyView!.view) 
    replyView!.didMoveToParentViewController(self) 
+0

仕組みは?このコードをどこで呼び出すべきですか? – Mannopson

+1

あなたは 'ViewController'を変更したいと思っています。そのボタンアクションがボタンアクション内で上記のコードを呼び出すと、 –

+0

ありがとうございます! – Mannopson

関連する問題