ios - 如何将 UIButton 连接到选项卡栏 Controller 中的特定选项卡?

标签 ios swift ios8 uitabbarcontroller uitabbar

我有一个有 2 个选项卡的 UITabBarController。每个选项卡都不同,但都可以选择在顶部发布新帖子。当我在任一 View 上点击新的 post 按钮时,我都有相同的 ViewController Present Modally。如果我点击 cancel 按钮,它会消失并返回到正确的选项卡,没问题。但是,如果我点击 post 按钮,我希望它始终转到第一个选项卡。

我如何以编程方式告诉它切换到之前的第一个选项卡

self.dismissViewControllerAnimated(true, completion: nil) 

这是我的 Storyboard 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/

相关文章:

ios - 代码签名错误如何修复找不到团队 ID

ios - 带有 TCP 的 Swift 网络框架

ios - 如何根据自动缩小的标签为多个标签设置相同的字体比例?

ios - 防止硬件(蓝牙)键盘覆盖 iOS 8 中的 inputView

ios8 - SDWebImage setImageWithURL iOS8崩溃

android - 不涉及第 3 方应用程序时需要 OAUTH2 吗?

ios - 在 iOS 上使用 Cordova WKWebView 插件时,Ajax 调用失败,HTTP 状态为 0

ios - 在 Swift 中解析 XML 文件

ios - 将照片应用程序添加到 "Open in"菜单 - IOS?

ios - 将 View 置于最前面,即使它的层次结构顺序不正确