【问题标题】:How to pass data from one tabbar to another in swift?如何快速将数据从一个标签栏传递到另一个标签栏?
【发布时间】:2016-03-07 23:25:38
【问题描述】:

我想将数据从一个 tabBar 控制器传递到另一个,

我正在使用tabBarController?.selectedIndex = 0切换tabBar

我尝试了很多选项,但无法将数据传递到另一个选项卡,有没有像我们使用导航控制器传递数据这样的简单解决方案?

【问题讨论】:

  • 您可以随时使用 NSNotificationCenter 将数据发送到任何地方。你考虑过使用它吗?
  • 是的,通知和 nsuserdefault 是一种方式,但认为应该有另一种方式来做到这一点
  • 顺便说一句,导航控制器如何传递数据?

标签: ios swift2 uitabbarcontroller uitabbar


【解决方案1】:

选项卡通常是一些自定义的 UIViewController。从这些视图控制器(选项卡)中,您还可以通过以下方式访问 UITabBarController:

if let mainController = UIApplication.sharedApplication().delegate?.window??.rootViewController as? YourMainTabBarControllerClass {
    mainController.someVariable = 123
}

这里要小心,因为标签栏控制器可能不是rootViewController,详情见this question

为了在 UITabBarController 中存储一些属性,您必须通过扩展 UITabBarController 来实现自己的类,然后在 StoryBoard 中设置自定义类。该类将如下所示:

class YourMainTabBarControllerClass: UITabBarController {

    // some custom variables here...
    var someVariable = 0

    ...

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多