Ios 在UILabarController中,如何找出选项卡栏中的哪个UIViewController显示了Swift 2.x中的当前UIViewController
我有一个Ios 在UILabarController中,如何找出选项卡栏中的哪个UIViewController显示了Swift 2.x中的当前UIViewController,ios,swift,Ios,Swift,我有一个UITabBarController,带有3个选项卡 是一个MainViewController 是一个SummaryViewController 是一个MenuViewController SummaryViewController可以仅从底部栏上的选项卡显示,也可以从MenuViewController中的菜单按钮显示。从菜单查看控制器的按钮调用时,我所做的只是调用: tabBarController?.selectedIndex = 2 这是我的问题,我在SummaryViewC
UITabBarController
,带有3个选项卡
MainViewController
SummaryViewController
MenuViewController
SummaryViewController
可以仅从底部栏上的选项卡显示,也可以从MenuViewController
中的菜单按钮显示。从菜单查看控制器的按钮调用时,我所做的只是调用:
tabBarController?.selectedIndex = 2
这是我的问题,我在SummaryViewController
中有一个CLOSE按钮,可以返回显示上述视图的UIViewController
。这就是我想要创造的效果
换句话说,,如果用户单击视图底部的选项卡栏选项进入摘要视图控制器
,然后单击摘要视图控制器
中可用的关闭
按钮,我想使用selectedIndex=1
但是,如果用户单击MenuViewController
中可用的菜单按钮进入摘要视图控制器
,然后单击关闭
按钮,我希望将用户发送回MenuViewController
因此,我如何在我的uitabarcontroller
中找到调用SummaryViewController
的UIViewController
,以便在单击CLOSE后切换到相应的UIViewController
。谢谢您如何使用NSUserDefaults
,创建UIAbbarController
的自定义子类,或者只显示一个新的UIViewController
实例,而不是切换选项卡以获得所需的输出
有很多方法可以解决你遇到的这个问题
NSUserDefaults:切换选项卡之前,保存当前选项卡控制器的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
}
在序列图像板/XIB中,将UITabBarController子类化为SomeTabBarController
presentViewController:创建要始终显示的ViewController的新实例
self.presentViewController(SomeViewController())
// when you click the close button inside `SomeViewController` just call
dismissViewControllerAnimated(true, completion: nil)
self.presentViewController(SomeViewController())
// when you click the close button inside `SomeViewController` just call
dismissViewControllerAnimated(true, completion: nil)