【问题标题】:Swift 3: UITabBarController change tab on unwindSwift 3:UITabBarController 在展开时更改选项卡
【发布时间】:2017-08-10 11:50:53
【问题描述】:

我有 UITabBarController。从 Home 标签,我转到 ThankYouVC

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

我尝试过的:

HomeVC

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

控制台日志打印unwind,但不改变 索引

另一个尝试:

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
}

还是没有运气!!

帮帮我。

【问题讨论】:

  • 你的ThankYouVC 怎么样?它是模态的“现在”转场吗?您的 TabBarController 是否在 NavigationController 中,并且您正在从 TabBar 推到 ThankYouVC
  • 我认为你的 self.tabBarController 是 nil ,这就是你的问题的原因。检查这个
  • @DonMag 我正在使用 Show(例如 Push)segue 转至ThankYouVC。不,TabBar 不在导航控制器内。
  • @MichałKwiecień,self.tabBarController 不是零。我在另一种情况下更改选项卡,效果很好

标签: ios swift swift3 uitabbarcontroller unwind-segue


【解决方案1】:

问题在于unwind segue 展开到拥有该函数的视图控制器。所以这就是你的结局。

一种解决方案:子类 UITabBarController 并将您的 unwind segue 放在那里。

class MyTabBarController: UITabBarController {

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

}

所以,将该类添加到您的项目中...将您当前 UITabBarController 的自定义类设置为MyTabBarController...将您的 Exit / Unwind segue 分配给这个新的,不要忘记删除您现有的 @ 987654325@函数和展开连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多