我有一个有 2 个选项卡的 UITabBarController
。每个选项卡都不同,但都可以选择在顶部发布新帖子。当我在任一 View 上点击新的 post
按钮时,我都有相同的 ViewController
Present Modally。如果我点击 cancel
按钮,它会消失并返回到正确的选项卡,没问题。但是,如果我点击 post
按钮,我希望它始终转到第一个选项卡。
我如何以编程方式告诉它切换到之前的第一个选项卡
self.dismissViewControllerAnimated(true, completion: nil)
这是我的 Storyboard
这是我点击发布按钮时调用的代码:
@IBAction func accept(sender: AnyObject) {
var currentUserLocation = getUserLocation()
currentUserLocation.getUserCoordinates()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var tabBarController = appDelegate.window!.rootViewController as! UITabBarController
tabBarController.SelectedIndex = 0
self.dismissViewControllerAnimated(true, completion: nil)
}
我最终遇到了错误:
'UITabBarController' does not have a member named 'SelectedIndex'
最佳答案
具有以下内容:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var tabBarController = appDelegate.window!.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0
索引 0 处的 Controller 对应最左侧的选项卡,索引 1 处的 Controller 对应右侧的下一个选项卡,依此类推。
关于ios - 如何将 UIButton 连接到选项卡栏 Controller 中的特定选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891808/