2017-05-15 4 views
0

私はUITabBarController3 Tabsを持っています。UITabBarControllerでは、Swift 2.xで現在のUIViewControllerを表示しているタブバーのどのUIViewControllerを見つけることができますか

  1. MainViewController
  2. ですSummaryViewController
  3. ですSummaryViewControllerは、下のバーの上にちょうどTabsから提示されるか、またはそれがまたから提示することができMenuViewController

ですメニューボタンMenuViewControllerです。 MenuViewControllerのボタンから呼び出されたときに私がやっているすべての呼び出しです:

tabBarController?.selectedIndex = 2 

はここに私の問題だ、私は言ったビューを提示UIViewControllerに戻りますSummaryViewControllerで利用可能CLOSEボタンを持っています。それが私が作りたいと思う効果です。言い換えれば

、ユーザーがSummary View Controllerに到達するためにビューのTab Bar options at the bottomをクリックしてから、私は戻ってselectedIndex = 1を使用してMain View Controllerをユーザーに送りたいSummary View Controllerで利用可能なCLOSEボタンをクリックした場合。ユーザーがSummary View Controllerに取得するMenuViewControllerで利用可能なメニューボタンをクリックして、CLOSEボタンをクリックした場合

しかし、私は戻ってMenuViewControllerをユーザーに送信したいと思います。したがって

、私は私のUITabBarControllerUIViewControllerは、彼らがCLOSEをクリックするとSummaryViewControllerはので、私は、対応するUIViewControllerに切り替えることができますと呼ばれるかを調べることができますか。ありがとう

答えて

0

NSUserDefaultsを使用するか、UITabBarControllerのカスタムサブクラスを作成するか、タブを切り替える代わりに新しいUIViewControllerインスタンスを表示するだけですか?

この問題にはさまざまな方法があります。

NSUserDefaults:タブはたとえば、現在のtabBarControllerのselectedIndex

を保存切り替える前に:

let key = "SomeTabBarControllerSelectedIndexKey" 
let defaults = NSUserDefaults.standardUserDefaults() 

defaults.setInteger(tabBarController!.selectedIndex, forKey: key) 
// then set the new index 
tabBarController?.selectedIndex = 2 // this is the new index assuming the previous index was 0 or 1 

// then when **CLOSE** is clicked you just set the tabBarController's index to the one you set in the UserDefaults 
if let savedIndex = defaults.integerForKey(key) { 
    tabBarController?.selectedIndex = savedIndex 
} 

UITabBarControllerサブクラス

// first we create the new class 
class SomeTabBarController: UITabBarController { 
    var previousIndex = 0 

} 

あなたは意志として使用その後、サブクラスを作成します。あなたのStoryboard/XIBサブクラスでは、UITab SomeTabBarController

enter image description here

(tabBarController as! SomeTabBarController).previousIndex = tabBarController!.selectedIndex 
// then set the new index 
tabBarController?.selectedIndex = 2 // this is the new index assuming the previous index was 0 or 1 

// then when **CLOSE** is clicked you just set the tabBarController's index to the one you set in the the class 
tabBarController?.selectedIndex = (tabBarController as! SomeTabBarController).previousIndex 

presentViewControllerにBarController:あなたはすべての時間に

self.presentViewController(SomeViewController()) 

// when you click the close button inside `SomeViewController` just call 
dismissViewControllerAnimated(true, completion: nil) 
を提示したいのViewControllerの新しいインスタンスを作成します。
関連する問題