Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UILabarController中,如何找出选项卡栏中的哪个UIViewController显示了Swift 2.x中的当前UIViewController_Ios_Swift - Fatal编程技术网

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)