我在以编程方式将用户从一个 View Controller 发送到另一个 View Controller 时遇到问题。我在下面发布了与他的流程相关的代码。除了让我知道正确的代码是什么(我将不胜感激)之外,如果逻辑/设计本身看起来不错,我也会感兴趣。
我正在以编程方式控制我的用户界面。因此,在我的应用程序委托(delegate)中,我有 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
}
}
我确定我忽略了一些愚蠢的事情,但有时需要另一双眼睛才能指出来。
提前致谢。
最佳答案
这里
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()
最好在 window
可见之前将 rootViewController
分配给它。否则可能会出现屏幕闪烁。
关于ios - 使用 navigationController?.pushViewController (Swift 3.0) 获取黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42979379/