ios - 模拟双击 UITabBarController 到 Root View Controller

标签 ios swift

对于我的应用程序,我为 iPad 制作了一个自定义菜单,以替换我用于 iPhone 的 UITabBarController。它工作得很好,除了当我单击一个选项卡时它会将我带到该选项卡并记住状态。我希望它带我到选项卡的主导航 Controller (UITabBarController 的直接子级,其中有四个),忘记我上次在哪里,就像在 native 选项卡栏上按两次时发生的情况一样元素。有什么想法吗?

在 AppDelegate.swift 中我有

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad{
 this_is_an_ipad = true
}

在 MainTabBarController.swift 中我有

class MainTabBarController: UITabBarController {
 override func viewWillLayoutSubviews(){
    if this_is_an_ipad{
        self.tabBar.hidden = true
    }
}

然后我的 SideMenuViewController.swift 是

import UIKit
class SideMenuViewController: UIViewController {

  @IBOutlet weak var homeButtonView: UIView!
  @IBOutlet weak var searchButtonView: UIView!
  @IBOutlet weak var savedButtonView: UIView!
  @IBOutlet weak var categoriesButtonView: UIView!

  var buttonColor = UIColor(red: 107.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 0.8)

  override func viewDidLoad() {
    super.viewDidLoad()
    homeButtonView.backgroundColor = buttonColor
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  @IBAction func homePressed(sender: AnyObject) {
    let tabBarController: UITabBarController? = self.parentViewController?.childViewControllers[1] as? UITabBarController
    tabBarController?.selectedIndex = 0
    unsetBackgroundColors()
    homeButtonView.backgroundColor = buttonColor
  }
  @IBAction func searchPressed(sender: AnyObject) {
    let tabBarController: UITabBarController? = self.parentViewController?.childViewControllers[1] as? UITabBarController
    tabBarController?.selectedIndex = 1
    unsetBackgroundColors()
    searchButtonView.backgroundColor = buttonColor
  }

  @IBAction func savedPressed(sender: AnyObject) {
    let tabBarController: UITabBarController? = self.parentViewController?.childViewControllers[1] as? UITabBarController
    tabBarController?.selectedIndex = 2
    unsetBackgroundColors()
    savedButtonView.backgroundColor = buttonColor
  }

  @IBAction func categoriesPressed(sender: AnyObject) {
    let tabBarController: UITabBarController? = self.parentViewController?.childViewControllers[1] as? UITabBarController
    tabBarController?.selectedIndex = 3
    unsetBackgroundColors()
    categoriesButtonView.backgroundColor = buttonColor
  }

  func unsetBackgroundColors(){
    homeButtonView.backgroundColor = UIColor.clearColor()
    searchButtonView.backgroundColor = UIColor.clearColor()
    savedButtonView.backgroundColor = UIColor.clearColor()
    categoriesButtonView.backgroundColor = UIColor.clearColor()

  }
}

最佳答案

想通了:

let nav = tabBarController?.childViewControllers[desiredIndex] as! UINavigationController
nav.popToRootViewControllerAnimated(true)

因此 SideMenuViewController.swift 的相关部分变为:

func tabPressed(desiredIndex: Int){
    let tabBarController: UITabBarController? = self.parentViewController?.childViewControllers[1] as? UITabBarController
    let selectedIndex = tabBarController?.selectedIndex
    if desiredIndex != selectedIndex{
        tabBarController?.selectedIndex = desiredIndex
        unsetBackgroundColors()
    }
    else{
        let nav = tabBarController?.childViewControllers[desiredIndex] as! UINavigationController
        nav.popToRootViewControllerAnimated(true)
    }
}

@IBAction func homePressed(sender: AnyObject) {
    tabPressed(0)
    homeButtonView.backgroundColor = buttonColor
}
@IBAction func searchPressed(sender: AnyObject) {
    tabPressed(1)
    searchButtonView.backgroundColor = buttonColor
}

@IBAction func savedPressed(sender: AnyObject) {
    tabPressed(2)
    savedButtonView.backgroundColor = buttonColor
    AppDelegate().softResetBadgeCount()
}

@IBAction func categoriesPressed(sender: AnyObject) {
    tabPressed(3)
    categoriesButtonView.backgroundColor = buttonColor
}

关于ios - 模拟双击 UITabBarController 到 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114203/

相关文章:

ios - 如何快速将数组映射到字典中的键?

iOS Firebase - 在模拟器中崩溃但不是设备 w/persistence

iOS清空请求队列

Swift - ADBannerView

ios - 没有得到 iPhone 屏幕边界的正确高度

ios - Xcode 7.3 : import Module displayed with strikethrough

ios - Swift - 以编程方式实例化 View Controller 时缺少元素

ios - 如何在 iOS 中修复 ‘TIC SSL Trust Error’?

iphone - AudioSessionInitialize 使用另一个中断监听器

swift - 如何使用调用站点上下文来报告/传播 Firestore 错误至 recordError