0

我有一个 swift 应用程序,在我的 UIViewController 上有一个按钮。在我的StoryBoard我将按钮附加到UITabController现在,当用户单击它时 - 他被重定向到它。但是,默认情况下,他会看到第一个选项卡。有没有办法显示第三个标签?

这是我的选择:

在此处输入图像描述

4

1 回答 1

1

是的——但它需要有多复杂取决于你在做什么。

如果您只从第一个开始,UIViewController那么您可以简单地向viewWillAppearorviewWillLoad函数添加一些代码(记住索引是从零开始的)

override func viewWillAppear(animated: Bool)
{
    self.selectedIndex = 2
}

如果您有多个入口点,则可以使用.prepareForSeguetabBarController. 在此示例中,我有两个按钮,其UIViewControllertag设置为 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
    }
}
于 2016-04-12T16:02:03.110 回答