我遇到了以编程方式将用户从一个视图控制器发送到另一个视图控制器的问题。我发布与他下面的流程相关的代码。除了让我知道正确的代码是什么(我会欣赏),如果逻辑/设计本身似乎没问题,我也会感兴趣。
我正在以编程方式控制我的UI。因此,在我的应用程序代表didfinnishlaunchingwithoptions我
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
FIRApp.configure()
window?.rootViewController = SigninController()
当用户打开应用程序时,它们会被重定向到SigninController。 然后,在SigninController内部,我正在处理针对Firebase的所有社交身份验证内容。我的代码中有一个监听器,用于确认用户是否(或未经过)身份验证并将其发送给他:
let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFacebookAuth()]
FUIAuth.defaultAuthUI()?.providers = provider
// listen for changes in the authorization state
_authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in
// check if there is a current user
if let activeUser = user {
// check if the current app user is the current FIRUser
if self.user != activeUser {
self.user = activeUser
self.signedInStatus(isSignedIn: true)
print("user session is active, redirecting...")
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
}
} else {
// user must sign in
self.signedInStatus(isSignedIn: false)
self.loginSession()
}
}
}
在上面的代码中,如果用户被确认为已登录,则我使用以下代码发送它们。这就是我遇到问题的地方。现在我只看到一个黑屏,但没有错误信息。
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
这是CUstomTabBarController类的代码。
class CustomTabBarController : UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
let home = createNavController(imageName: "gen-home", rootViewController: HomeController(collectionViewLayout: UICollectionViewFlowLayout()))
let loc = createNavController(imageName: "loc-map-route", rootViewController: LocController(collectionViewLayout: UICollectionViewFlowLayout()))
let stats = createNavController(imageName: "pre-bar-chart", rootViewController: StatsController(collectionViewLayout: UICollectionViewFlowLayout()))
let profile = createNavController(imageName: "account", rootViewController: ProfileController(collectionViewLayout: UICollectionViewFlowLayout()))
viewControllers = [home, loc, stats, profile]
}
private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
}
我确信我忽略了一些愚蠢的东西,但有时需要用另一双眼睛来指出它。
提前致谢。
答案 0 :(得分:2)
下面
self.navigationController?.pushViewController(nextViewController, animated: true)
没有navigationController
。
在AppDelegate
中,你需要这样做:
let rootViewController = SigninController()
let navController: UINavigationController = UINavigationController(rootViewController: rootViewController)
FIRApp.configure()
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
最好将rootViewController
分配给window
,然后再将其显示出来。否则屏幕可能会闪烁。