要跳过登录(如果用户已登录),我正在使用用户默认值,并且在应用程序委托中,我正在调用主视图控制器,但问题是数据没有像错误一样显示为nil错误Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value in swreal view controller
这里是我的代码
if UserDefaults.standard.bool(forKey: "login") {
//YES Already Login
self.window = UIWindow(frame:UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigationController = UINavigationController.init(rootViewController: viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
} else {
//NOT Login
self.window = UIWindow(frame:UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "signinpage") as! ViewController
let navigationController = UINavigationController.init(rootViewController: viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
,然后跳过登录到第3行中显示的home nil值的操作。自我看法...
super.viewDidLoad()
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
@IBAction func logoutButton(_ sender: Any) {
let alert = UIAlertController(title: "Alert", message: "Are you Sure You want to Logout", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
GIDSignIn.sharedInstance().signOut()
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "signinpage") as! ViewController
UserDefaults.standard.set(false, forKey: "login")
self.navigationController?.setViewControllers([secondViewController], animated: true)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler:nil))
self.present(alert, animated: true, completion: nil)
}
答案 0 :(得分:0)
首先检查您的故事板ID ,是否正确。您可以检查以下代码以获取更多信息。希望它可以帮助您解决问题。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if UserDefaults.standard.object(forKey: "login") != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: HomeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let mainViewController = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let navigationController = storyboard.instantiateViewController(withIdentifier: "NavigationController") as! NavigationController
navigationController.setViewControllers([viewController], animated: false)
mainViewController.rootViewController = navigationController
let window = UIApplication.shared.delegate!.window!
window!.rootViewController = mainViewController
UIView.transition(with: window!, duration: 0.3, options: [.transitionCrossDissolve], animations: nil, completion: nil)
}
}
答案 1 :(得分:0)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Check that user login or not
if let isLogin = UserDefaults.standard.object(forKey: "login") as? Bool {
//If already login then show Dashboard screen
self.showDashboardScreen()
} else {
//If not login then show Login screen
self.showLoginScreen()
}
return true
}
func showLoginScreen() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginViewController = storyboard.instantiateViewController(withIdentifier: "#LoginViewControllerStoryboardID") as! LoginViewController
var navigationController = UINavigationController()
navigationController = UINavigationController(rootViewController: loginViewController)
//It removes all view controllers from the navigation controller then sets the new root view controller and it pops.
window?.rootViewController = navigationController
//Navigation bar is hidden
navigationController.isNavigationBarHidden = true
}
func showDashboardScreen() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let dashboardViewController: DashboardViewController = storyboard.instantiateViewController(withIdentifier: "#DashboardViewControllerStoryboardID") as! DashboardViewController
var navigationController = UINavigationController()
navigationController = UINavigationController(rootViewController: dashboardViewController)
//It removes all view controllers from the navigation controller then sets the new root view controller and it pops.
window?.rootViewController = navigationController
//Navigation bar is hidden
navigationController.isNavigationBarHidden = true
}
答案 2 :(得分:0)
根据我的观察,
self.revealViewController()
为零。为了确保它是否为零,请在第三行添加一个断点,并通过在控制台中打印self.revealViewController()
po self.revealViewController()
如果为零, 请参阅以下链接:
Self.revealViewController() returns nil
https://github.com/John-Lluch/SWRevealViewController/issues/627
另外,显示您用于配置SWRevealcontroller的代码。
答案 3 :(得分:0)
更改此代码
self.window = UIWindow(frame:UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigationController = UINavigationController.init(rootViewController: viewController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
对此:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
假设您有一个类型为SWRevealViewController
的导航控制器,其sw_front
上有HomeViewController
标记,或者前面有一个NavigationController,而菜单VC上有sw_rear
。因此,您必须实例化SWRevealViewController
才能显示菜单,因为您的代码直接进入主屏幕,因此SWRevealViewController
为nil,因为它没有在HomeViewController
之前实例化。
答案 4 :(得分:0)
您可以使用多故事板概念。
家庭情节提要
+(void) LoginHomeScreen:(NSString*)strController andIndex:(int)index
{
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:USERDETAILS];
NSDictionary *userData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (userData != NULL && [userData count] > 0) {
//DashboardVC
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"SideMenuNavigationVC"];
[navigationController setViewControllers:@[[storyboard instantiateViewControllerWithIdentifier:strController]]];
LGSideMenuController *mainViewController = [storyboard instantiateInitialViewController];
mainViewController.rootViewController = navigationController;
UIViewController * SideMenu = [storyboard instantiateViewControllerWithIdentifier:@"SideMenuVC"];
mainViewController.leftViewController = SideMenu;
mainViewController.leftViewPresentationStyle = LGSideMenuPresentationStyleSlideBelow;
UIWindow *window = UIApplication.sharedApplication.delegate.window;
window.rootViewController = mainViewController;
[UIView transitionWithView:window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];
} else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
LoginVC *loginVC = (LoginVC *)[storyboard instantiateViewControllerWithIdentifier:@"LoginVC"];
UINavigationController *navLogin = [[UINavigationController alloc] initWithRootViewController:loginVC];
navLogin.navigationBar.hidden = true;
[[UIApplication sharedApplication].delegate.window setRootViewController:navLogin];
}
}