I have home view controller with ENSideMenu for side menu and in home view controller i have sign out button. I have given ENSideMenu MyNavigationController as an is initial view controller in storyboard.
I want to show home first in app which is working fine but when i come from login to home then home is not responding.. once i sign out then again home is responding.
Here is my code:
in appdelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let userId: String? = KeychainWrapper.standard.string(forKey: "Uid")
print("appdelegate userid \(userId)")
if userId != nil{
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
self.window!.rootViewController = homeVC
}
return true
}
in loginButton in LogInVC:
let saveUserId: Bool = KeychainWrapper.standard.set(Uid ?? "", forKey: "Uid")
print("the userid is \(saveUserId)")
if (Uid?.isEmpty)!
{
print("login fail")
}
else{
DispatchQueue.main.async {
let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let appDelagate = UIApplication.shared.delegate
appDelagate?.window??.rootViewController = homeVC
}
}
in HomeVC:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "MakePaymentViewController") as! MakePaymentViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
let indexPathHome = indexPath.row
print("home collectionItem indexpath \(indexPathHome)")
}
@IBAction func signOutButton(_ sender: Any) {
print("signout tapped")
KeychainWrapper.standard.remove(key: "Uid")
}
@IBAction func sideMenuButton(_ sender: Any) {
print("in side menu")
toggleSideMenuView()
}
I have given ENSideMenu MyNavigationController as an is initial view controller in storyboard.
I need homeVc to respond after coming from login. please help me to solve the issue.
You are using custom MyNavigationController
with HomeViewController
for navigation purposes - you already mention, that it is your initial view in storyboard. But... there are two other places, where you are loading HomeViewController
without MyNavigationController
:
AppDelegate
change loading HomeViewController
:
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MyNavigationController")
LoginViewController
Change
let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let navigationController = UINavigationController(rootViewController: homeVC)
to
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = mainStoryBoard.instantiateViewController(withIdentifier: "MyNavigationController")
Also, I have some advice for you:
!
in your code - this is straight way to crash your app. There are lot of other ways to deal with optionalsself
in closures (like in LoginViewController
, when you are changing your view to HomeVC). This cause retain cycle, which leads to memory leak.self
s - and when compilator tells you, that you need to add self
- look at point 2) and add weak self
instead of strong
.