我有一个快速的应用程序,在我的UIViewController上我有一个按钮。在我的StoryBoard
我将按钮附加到UITabController
,现在当用户点击它时,他会被重定向到它。但是,默认情况下,他会看到第一个选项卡。有没有办法显示第三个标签?
这是我的选择:
答案 0 :(得分:1)
是的 - 但它需要多么复杂取决于你正在做什么。
如果您只是从第一个UIViewController
开始,那么您只需向viewWillAppear
或viewWillLoad
函数添加一些代码(记住索引从零开始)
override func viewWillAppear(animated: Bool)
{
self.selectedIndex = 2
}
如果您有多个入口点,则可以使用prepareForSegue
在tabBarController
中设置标记。在此示例中,UIViewController
上有两个按钮,tag
值设置为100,而200
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TabBarSegue"
{
if let destinationVC = segue.destinationViewController as? myTabBarViewController
{
if sender!.tag == 100
{
destinationVC.jumpToTab2 = true
}
if sender!.tag == 200
{
destinationVC.jumpToTab2 = false
}
}
}
}
然后在TabBarController
中,我定义了一个标记jumpToTab2
class myTabBarViewController: UITabBarController
{
var jumpToTab2 : Bool = false
override func viewWillAppear(animated: Bool)
{
if jumpToTab2
{
self.selectedIndex = 2
}
jumpToTab2 = false // reset the flag before next time
}
}