【问题标题】:Load Data Before Adding ViewControllers to UITabBarController在将 ViewController 添加到 UITabBarController 之前加载数据
【发布时间】:2016-01-30 16:43:57
【问题描述】:

在我的应用程序中,一旦用户从LoginViewController 登录,他就会被定向到ProfileTabBarController

ProfileTabBarControllerUITabBarController 的子类。它由三个视图控制器组成,它们都需要引用 Profile 的实例。

ProfileTabBarController 被推送时,它会加载用户的个人资料。成功加载配置文件后,它会在其每个视图控制器上设置对配置文件的引用,然后将它们添加为选项卡项。

我选择这种方式的原因是,如果用户启动应用程序并且他的令牌没有过期,应用程序应该直接转到ProfileTabController;用户无需再次登录。与其在AppDelegateLoginViewController 中重复加载配置文件的代码,我觉得最好将其封装在ProfileTabBarController 中。

这种方法的问题在于,在 segue 期间,UITabBarController 显示为黑色,因为没有设置视图控制器。我设法通过创建LoadingViewController 并将其最初设置为ProfileTabController 中唯一的UIViewController 来解决此问题

我的问题是是否有更好的方法来解决这个问题。我真的不喜欢有一个 UIViewController 没有其他目的然后显示加载图标的想法。

【问题讨论】:

    标签: ios segue preloading uitabcontroller


    【解决方案1】:

    呈现一个没有视图的选项卡视图控制器没有多大意义,所以我认为你给它一个视图控制器来处理这种加载状态的解决方案非常有意义。加入一些很酷的动画,让用户保持愉悦。

    也有可能出现问题,配置文件可能无法正确加载。这个过渡视图控制器似乎是放置代码以处理可能的错误的好地方。

    【讨论】:

      【解决方案2】:

      我能够通过使用NSNotificationCenter 来消除Loading ViewController。

      • ProfileTabBarController 添加三个 ViewController,然后开始加载配置文件。

      覆盖 func viewDidLoad() { super.viewDidLoad()

          self.setupUI()
          self.setupViewControllers()
          self.loadProfile()
      

      }

      • 加载完成后,ProfileTabBarController 会发出 ProfileDidLoad 通知。

      let completion = { (profile: Profile?) in

      MBProgressHUD.hideHUDForView(self.view, animated: true)
      
      if let profile = profile {
      
          self.profile = profile
          NSNotificationCenter.defaultCenter().postNotificationName(Notification.ProfileDidLoad, object: self.profile)
      
      } else {
      
          UIAlertView(
              title: "",
              message: NSLocalizedString("Error loading profile", comment: ""),
              delegate: nil,
              cancelButtonTitle: "OK"
              ).show()
      
      }
      

      }

      • ViewController 观察ProfileDidLoad 通知:

        函数 setupNotificationObserver(){

            NSNotificationCenter.defaultCenter().addObserver(self, selector: "profileDidLoad:", name: Notification.ProfileDidLoad, object: nil)
        
        }
        
        @objc func profileDidLoad(notification: NSNotification){
        
            if let profile = notification.object as? Profile{
                self.profile = profile
            }
        
        }
        

      通过这种方式,视图控制器在登录后立即显示,无需加载屏幕。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多