swift - 如何以编程方式将 VC 放置在导航控制器中?
问题描述
每当我尝试展示此 VC 时,我目前都会遇到错误。错误是Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value我相信这是因为 VC 不在导航控制器中。如何将此 VC 放入导航控制器(以编程方式)。
import UIKit
import AWSAuthCore
import AWSAuthUI
import AWSMobileClient
class SignInViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AWSMobileClient.default()
.showSignIn(navigationController: self.navigationController!, // this is where i get the error.
signInUIOptions: SignInUIOptions(
canCancel: true,
logoImage: UIImage(named: "MyCustomLogo"),
backgroundColor: UIColor.red)) { (result, err) in
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
switch(userState){
case .signedIn:
DispatchQueue.main.async {
}
case .signedOut:
AWSMobileClient.default().showSignIn(navigationController: self.navigationController!, { (userState, error) in
if(error == nil){ //Successful signin
DispatchQueue.main.async {
}
}
})
default:
AWSMobileClient.default().signOut()
}
} else if let error = error {
print(error.localizedDescription)
}
}
}
}
}
这是来自的代码AppDelegate
import UIKit
import AWSAppSync
import AWSMobileClient
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var appSyncClient: AWSAppSyncClient?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let nav1 = UINavigationController()
let mainView = SignInViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: TabBarController())
window?.makeKeyAndVisible()
AWSMobileClient.default().addUserStateListener(self) { (userState, info) in
switch (userState) {
case .guest:
print("user is in guest mode.")
case .signedOut:
self.window?.rootViewController?.present(SignInViewController(), animated: true, completion: nil)
print("user signed out.")
case .signedIn:
print("user is signed in.")
case .signedOutUserPoolsTokenInvalid:
print("need to login again.")
case .signedOutFederatedTokensInvalid:
print("user logged in via federation, but currently needs new tokens")
default:
print("unsupported")
}
}
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}
do {
// You can choose the directory in which AppSync stores its persistent cache databases
let cacheConfiguration = try AWSAppSyncCacheConfiguration()
// AppSync configuration & client initialization
let appSyncServiceConfig = try AWSAppSyncServiceConfig()
let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: appSyncServiceConfig,
cacheConfiguration: cacheConfiguration)
appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
print("Initialized appsync client.")
} catch {
print("Error initializing appsync client. \(error)")
}
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
解决方案
嵌入SignInViewController
在一个UINavigationController
.
为此,将 a 拖放UINavigationController
到 theStoryboard
并CTRL + drag
从 that拖放UINavigationController
到SignInViewController
,选择关系rootViewController
以编程方式:
var nav1 = UINavigationController()
var mainView = SignInViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
推荐阅读
- react-native - 更改 react-native-elements 中的输入图标颜色
- node.js - AWS EC2 实例(生产)中的 PostGres 超时错误
- php - 此登录代码的 mysqli 等效项是什么?
- python - 如何解决在 Mac 上运行的 Python 中的导入错误?
- python-3.x - sqlite3 错误:您没有为绑定 1 提供值
- uwp - 媒体基础库中的 UWP AAC 编码,未注册类
- php - Bootstrap手风琴在选择另一个后不会折叠
- c# - C# 无法避免嵌套的 try-catch
- docker - 在 Docker 中为 Ubuntu 16.04 运行 Systemd,对于 Ubuntu 18.04 它可以工作
- javascript - 提交表单时通过 JS 验证号码