using Third Party Slider and couldn't navigate to another view - ios

i am currently using KYDrawerController for a slider menu and I couldn't navigate to another page. Whenever I press a button (even without codes in it), it crashes. Another is i tried to set another view as initial view, it doesnt shows that. I am suspecting it's something to do with my appDelegate file. I need some troubleshooting help around if possible. I changed my AppDelegate file to
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var drawerController = KYDrawerController.init(drawerDirection: .left, drawerWidth: 300)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let mainVC = storyboard.instantiateViewController(withIdentifier: "MainMenu")
let menuVC = storyboard.instantiateViewController(withIdentifier: "drawer")
self.drawerController.mainViewController = mainVC
self.drawerController.drawerViewController = menuVC
self.window?.rootViewController = self.drawerController
self.window?.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
anything here might have been the problem?

Your are missing to embed your view controller in navigationController. Please update your code for mainVC as follows and try again.
let mainVC = storyboard.instantiateViewController(withIdentifier: "MainMenu")
let mainViewNavigationController = UINavigationController(rootViewController: mainVC)
self.drawerController.mainViewController = mainViewNavigationController
I hope this will fix your issue. Let me know if still facing any issue.

Related

Could not cast value of view controllers error

My app was building fine until today when all of a sudden I got the following errors after a successful build:
Could not cast value of type 'FirebaseApp.HomeViewController' (0x101ab6aa0) to 'FirebaseApp.MenuViewController' (0x101ab6d30).
Could not cast value of type 'FirebaseApp.HomeViewController' (0x101ab6aa0) to 'FirebaseApp.MenuViewController' (0x101ab6d30).
The line in my app delegate
let controller = storyboard.instantiateViewController(withIdentifier:
"mainView") as! MenuViewController
was then highlighted red within this AppDelegate Class:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
// Override point for customization after application launch.
let authListener = Auth.auth().addStateDidChangeListener { auth, user in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if user != nil {
//
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
} else {
// main screen
let controller = storyboard.instantiateViewController(withIdentifier: "mainView") as! MenuViewController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
}
return true
}
I have in my storyboard the mainView identified:
And I have a HomeViewController
class HomeViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {
which has the bulk of my code.
I am not sure what went wrong here, but I suspect it has something to do with misnaming in my story board.
Edit: I solved this problem by changing "mainView" to "MenuViewController"
In the storyboard for MenuViewController you need to set mainView as Storyboard ID not the Restoration ID.
Like below image:

How to go to the what I want controller when application didFinishLaunching

When I launching my application, I want go to the custom view controller, but the default first controller in storyboard:
I want go to brown vc when launch my app:
My method is below, but get a black screen with Unable to capture view hierarchy
1) In Info.plist, delete the Main storyboard file base name line.
2) In my Appdelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let sb:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav: CustomNavController = sb.instantiateViewController(withIdentifier: "CustomNavController") as! CustomNavController
let red_vc:ViewController = sb.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let green_vc:ViewController2 = sb.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
let brown_vc:ViewController3 = sb.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
nav.viewControllers = [red_vc, green_vc, brown_vc]
self.window? = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
Result get a black screen:
Is which step I mistake? and how can I get it well ? When launch my application I want go to brown vc.
ATTEMPT - 1
Using push to brown vc, it is not work too:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let sb:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav: CustomNavController = sb.instantiateViewController(withIdentifier: "CustomNavController") as! CustomNavController
let red_vc:ViewController = sb.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let green_vc:ViewController2 = sb.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
let brown_vc:ViewController3 = sb.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
//nav.viewControllers = [red_vc, green_vc, brown_vc]
nav.pushViewController(red_vc, animated: false)
nav.pushViewController(green_vc, animated: false)
nav.pushViewController(brown_vc, animated: false)
self.window? = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
You no need delete Main storyboard file base name in Info.plist
In AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let brownVC = mainStoryboard.instantiateViewController(withIdentifier: "brownVCIdentifier") as! BrownViewController
let navigationController = application.windows[0].rootViewController as! UINavigationController
navigationController.isNavigationBarHidden = true
navigationController.pushViewController(brownVC, animated: false)
return true
}
(or)
In RedViewController(first viewController):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let brownVC = storyboard?.instantiateViewController(withIdentifier: "brownVCIdentifier") as! BrownViewController
navigationController?.isNavigationBarHidden = true
navigationController?.pushViewController(brownVC, animated: false)
}
Github link:
https://github.com/k-sathireddy/DirectNavigationSample
You are setting the root viewcontroller as nav which has no UIView and it's creating a blank view due to which you see the black view when app is launched.
this code nav.viewControllers = [red_vc, green_vc, brown_vc] is not making any sense as you cannot set view controllers like this for UINavigationController. Navigation controller works with PUSH/POP mechanism hence you need to push the view controllers on nav controller which will correctly setup the view hierarchy. Push the controllers to brown_vc in nav and it should work.

How to Pass Values from Separate Classes to AppDelegate?

I have a class in my SignInViewController.swift:
class CredentialState: NSObject {
static let sharedInstance = CredentialState()
var signedIn = false
var displayName: String?
var photoUrl: NSURL?
}
I would like to use the variable signedIn to authenticate users in AppDelegate with an if-else statement. Currently, I have a way to set the viewcontroller to CustomTabBarController (custom programmatically made) or SignInViewController (storyboard made). The if statement would basically say if the value is false set the controller to the sign in screen and if it's true then go to the tab bar screen.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
// window?.rootViewController = CustomTabBarController()
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
self.window?.rootViewController = loginVC
return true
}
if i understood you correctly:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// check login state
if CredentialState.sharedInstance.signedIn {
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
window?.rootViewController = CustomTabBarController()
} else {
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
window?.rootViewController = loginVC
}
window?.makeKeyAndVisible()
return true
}
I am not so sure of what you are asking, yet I'll try to answer that. Basically what you need to do is just simply have this piece of code above your CredentialState class:
credentialState : CredentialState = CredentialState()
In this way you can change or check your signedIn variable from AppDelegate. So simply in the AppDelegate file you can:
if(credentialState.signedIn == true) ...
Hope I was able to answer your question

iOS Swift SlideMenuControllerSwift

I am learning to develop iOS and come across to this awesome library SlideMenuController. I am able to make use of this library to create awesome slide menu easily.
However, there are some problem that I am facing during implementing with this library.
Environment
In my project storyboard, I have 1 TabBarViewController and 2 ViewController which is under this TabBarViewController. I also have 2 independent ViewController which are LoginViewController and LeftMenuViewController.
I have below code in my AppDelegate.swift file
...
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
let slideMenuController = SlideMenuController(mainViewController: mainViewController, leftMenuViewController: leftViewController)
SlideMenuOptions.contentViewScale = 1
SlideMenuOptions.hideStatusBar = false;
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
return true
}
...
Everything work perfectly if I don't need my login view as a initial view. I notice that this line of code self.window?.rootViewController = slideMenuController will always make sure that view in mainViewController to be the initial view when the app open.
How can I have my LoginView as the initial view when my app start up and the slide menu is still bind to the TabBarViewController and working properly?
I have try to move the code from AppDelegate.swift to viewDidLoad() function in TabBarViewController.swift but no luck. It is not working.
Need some guilds and help on this. Thanks.
you can use this code instead of yours :
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftMenuViewController") as! LeftMenuViewController
let loginViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc: UINavigationController = UINavigationController(rootViewController: loginViewController)
leftViewController.mainVC = nvc
let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftViewController)
SlideMenuOptions.contentViewScale = 1
SlideMenuOptions.hideStatusBar = false;
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()
return true
}
dont forget to add this variable in your LeftMenuViewController
var mainVC : UIViewController!

Correct way to show ViewController from push notification when app is closed

I'm trying to show a view controller when my app is open from a push notification, but it's crashing because I'm trying to get the UINavigationController, which is not still initialized in the AppDelegate didFinishLaunchingWithOptions function.
My code is this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let profileController = storyboard.instantiateViewControllerWithIdentifier("profileViewControllerIdentifier") as! ProfileViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(profileController, animated: false)
}
}
What is the correct way to show a ViewController when coming from a push notification when the app was closed?
So, the main reason I see is that your rootViewController is not a UINavigationController. It could be because:
rootViewController is another UIViewController
The UINavigationController is not set as the rootViewController in your storyboard.
So without any print about what are you getting from self.window?.rootViewController I think you can run this line to see if you can get your UINavigationController:
var myNav = storyboard.instantiateViewControllerWithIdentifier("myNavIdHere") as? UINavigationController
just make sure to add an ID to your UINavigationController. (this is just to check you can reach your NavController)
Just found what the problem was, I was accessing some data from the navigationcontroller, which was not still initialized.

Resources