Swift 3:UITabBarController 在展开时更改选项卡

Swift 3: UITabBarController change tab on unwind

我有 UITabBarController。从 Home 选项卡,我转至 ThankYouVC.

当我从 ThankYouVC 放松时,我想更改选定的选项卡。

我尝试过的:

首页VC

@IBAction func unwindToMain(segue:UIStoryboardSegue) {
    print("unwind")
    self.tabBarController?.selectedIndex = 0
}

控制台日志打印 unwind,但 不会更改 index

另一次尝试:

enum Notifications: String, NotificationName {
    case QRDoneNotification
}

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(unwindCallBack), name: Notification.Name("QRDoneNotification"), object: nil)
}

@IBAction func unwindToMain(segue:UIStoryboardSegue) {
    print("unwind")
    NotificationCenter.default.post(name: Notifications.QRDoneNotification.name, object: nil)
}

func unwindCallBack() {
    self.tabBarController?.selectedIndex = 0
}

仍然没有运气!!

帮帮我。

问题是 unwind segue 展开到包含该函数的视图控制器 。这就是你的结局。

一个解决方案:subclass UITabBarController 然后把你的 unwind segue 放在那里。

class MyTabBarController: UITabBarController {

    @IBAction func unwindToMain(segue:UIStoryboardSegue) {
        print("Unwinding to the custom tab bar controller...")
        selectedIndex = 1
    }

}

因此,将 class 添加到您的项目中...将当前 UITabBarController 的自定义 Class 设置为 MyTabBarController...将您的退出/展开转场分配给这个新的第一,不要忘记删除现有的 unwindToMain() 函数并解除连接。