ios swift - 以编程方式显示标签栏控制器

时间:2016-03-12 14:08:02

标签: ios swift tabs

我很难呈现不是根视图控制器的标签栏控制器。

我有当前的设置: enter image description here

我想按下主视图控制器中的按钮,并能够显示标签栏控制器,并返回主视图控制器。

我尝试创建一个类型为UITabBarViewController的类,将它与我的Tab Bar Controller相关联,只是呈现它但它不起作用。

我想提供标签栏控制器,并选择了收藏夹选项卡。

我尝试了什么:

let vc = TabBar()

        self.presentViewController(vc, animated: true, completion: nil)

2 个答案:

答案 0 :(得分:1)

您可以通过设置UITabBarController的选定索引属性来切换选项卡。像这样:

curl 'https://api.paypal.com/v1/oauth2/token' \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -H "Authorization: Basic QWZVa...==" \
    -d 'grant_type=refresh_token&refresh_token=MFYQ...'

如果你想要的只是在两个标签之间切换,你不需要创建新的视图控制器或执行segues。

答案 1 :(得分:1)

你可以这样做:

使用segue:

从按钮拖动到tabBarViewController并选择一个类型(Modal,Push(如果你的mainViewController是NavBarVC)...)

来自代码:

点击你的tabBarViewController并转到属性检查器并为你的VC提供一个故事板ID

并且来自代码:

let mainST = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let VC = mainST.instantiateViewControllerWithIdentifier("idTabBar")
presentViewController(VC, animated: true, completion: nil)

编辑关闭tabBar

如果tabBar以模态方式呈现, 要解雇它你有两个选择:

1)使用代表:

protocol ExitMe {
   func exitMe()
}

在tabBar的视图控制器演示器中

extension PresenterOfTabBar: ExitMe{
     func exitMe(){
         dismissViewControllerAnimated(false, completion: nil)
     }
}

并在tabBarViewController中定义一个exitDelegate变量var exitDelegate: ExitMe!并从演示者设置它的值。当用户单击按钮退出tabBar时,您只需拨打exitDelegate.exitMe()

使用segue进行模态化时使用unwindFuction:

在演示者中定义一个像这样的函数

@IBAction func unwindFromTabBar(sender: UIStoryboardSegue){
       // do what you want here
}

并在InterfaceBuilder中从应退出标签栏的按钮拖动到视图控制器停靠栏中的出口,然后选择func unwindFromTabBar。

可能存在其他解决方案(使用通知,获取prsenter View控制器....)您应该选择合适的解决方案......