0

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

ProfileTabBarController是 . 它的子类。UITabBarController它由三个视图控制器组成,所有这些视图控制器都需要对 Profile 实例的引用。

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

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

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

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

4

2 回答 2

1

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

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

于 2016-01-30T18:38:39.743 回答
0

我能够通过使用NSNotificationCenter.

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

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

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

}

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

让完成 = { (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()

}

}

  • ViewControllers 观察ProfileDidLoad通知:

    func 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
        }
    
    }
    

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

于 2016-01-31T19:34:25.250 回答