Reputation: 1541
How can I present a view controller from my AppDelegate and have a Navigation bar added to that view with a back button to the previous view? I need to do this programmatically from my AppDelegate. Currently I can push a controller from there, but it doesn't act like a segue. It doesn't add a nav bar with a back button. Now I know I should be able to add one myself, but when I do it gets hidden. Currently I'm using pushViewController()
, but I imagine that's not the best way to do it.
Upvotes: 0
Views: 189
Reputation: 933
I had something that I think is similar, if not the same:
HIGH LEVEL VIEW
UIViewController
(ViewController.swift) embedded in a UINavigationController
UIViewController
segue to a view with a custom class:ExistingLocationViewController
- subclass of:UITableViewController
UINavigationController
's Toolbar segues to view with another custom class:NewLocationViewController
- subclass of:UIViewController
CLLocationManagerDelegate
UITextFieldDelegate
RESOLUTION
override func viewDidLoad() {
super.viewDidLoad()
//...
navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated) // #=# not sure if this is needed
navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
}
You could actually omit the last two lines in viewWillDisappear
, or perhaps even omit the entire override
function
The net result (for me) was as depicted below:
Upvotes: 1
Reputation: 24714
If you want add a NavigationController
in appDelegate
you can do it like this,in this way,your viewcontroller is load from storyboard
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let vc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("vc") as! ViewController
let nav = UINavigationController(rootViewController: vc)
self.window?.rootViewController = nav
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
return true
}
Upvotes: 0