Programmatically create navigation controller with back button - ios

I haven't been able to figure something out in swift in my AppDelegate. I've been working on this little problem for weeks (in the background, not continuously) with lots of googling. I must be missing something.
When a remote notification comes in to my iOS app I'd like to modally present the View appropriate for the message, with a UINavigationController that has a Back button. The idea is that when the user is finished dealing with the notification, they can press "Back" and go back to where they were. I can't get a "Back" button to show up in my navigation controller programmatically.
Not all of my existing views are embedded in navigation controllers. So, rather than finding an existing UINavigationController in the stack, I've been creating a new UINavigationController and trying to set it up and present it like this:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mvc = mainStoryboard.instantiateViewControllerWithIdentifier("MyViewIdentifier") as! MySpecialViewController
/* here is where I setup the necessary variables in the mvc view controller, using data from the remote message. You don't need to see all that */
/* this seems to work, I do get what looks to be a navigation controller */
let newNavController = UINavigationController(rootViewController: mvc)
/* this part doesn't work, I don't get a "Back" button */
let btn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Done, target: mvc, action: "backPressed")
newNavController.navigationItem.leftBarButtonItem = btn
/* this block seems to work. The idea is to work through the hierarchy of any modal presentations
until we get to the screen that is actually showing right now. I'm not
sure this will work in all situations, but it seems to work in my app's hierarchy. */
var currentViewController = self.window!.rootViewController
while currentViewController?.presentedViewController != nil {
currentViewController = currentViewController?.presentedViewController
}
/* this I suppose shows my new view controller modally, except without
the "back" button it's hard to be sure the navigation controller is even there. */
currentViewController?.presentViewController(newNavController, animated: true, completion: nil)
I do have a method called "backPressed" in the relevant view controller.
Any clues as to why the navigation bar button isn't showing up?
Edit: I knew that backBarButtonItem only shows up when we aren't at the top of the stack. But, leftBarButtonItem should show up even if we're at the top of the stack?

Here is simple example for you which can set navigation bar Programmatically from first View:
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SecondView") as? SecondView {
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with resultController at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
If you wan to add back button into that navigation then use this code into SecondView.swift class:
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}
If you want to do all of this from firstView then here is your code:
#IBAction func btnPressed(sender: AnyObject) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SecondView") as? SecondView {
resultController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
let navController = UINavigationController(rootViewController: resultController) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)
}
}
func goBack(){
dismissViewControllerAnimated(true, completion: nil)
}
Hope this will help you.

I take it that your view controller is used by more than one segue?
Within storyboard, you can embed the view controller in a navigation controller.
For the modal presentation, you segue to the navigation controller instead of the view controller. This will supply a bar for the modal presentation.
A Done button is more likely used to dismiss a modal presentation. (Users might be confused by that atypical use of a back button, as your presented navigation controller is not popping a view controller off its stack.)

Related

Unable to add barbuttonitem to navigation controller

I would like to integrate both navigation controller and tab bar controller in my project.But I am unable to add right barbutton to the navigation controller.
I have attached the screenshot of the storyboard
What I have done is I have added navigation controller to login screen and this time I am able to add barbuttonitem both by adding code as well as by dragging barbuttonitem to navigation controller.
let addBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
self.navigationItem.rightBarButtonItem = addBtn
Problem I am facing is after adding Tab bar controller I am unable to add rightbarbutton both by code as well as by dragging to the navigation controller. please help me.
When a ViewController is embedded in a NavigationController you use
self.navigationItem.rightBarButtonItem = addBtn
In your project Detail Screen isn't embedded in NavigationController directly. Detail Screen is embedded in TabBarController, TabBarController is embedded in NavigationController. So you should use
self.tabBarController?.navigationItem.rightBarButtonItem = addBtn
But this addBtn will be visible in all view controllers which are embedded in the TabBarController.
If you want to add the rightBarButton for only one viewcontroller, then embed the Detail Screen in a new NavigationController. Then you can add rightBarButton using
self.navigationItem.rightBarButtonItem = addBtn
You should be sure parent returns the top child controller of UINavigationController. In my case
parent?.parent?.navigationItem.right...
did the trick.
If you reuse the controller -as embedded or not- which you want add items to navigationItem, following example will work. However some logical changes may be needed.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = UIBarButtonItem()
return
}
navigationItem.rightBarButtonItem = UIBarButtonItem()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = nil
return
}
navigationItem.rightBarButtonItem = nil
}

Navigation bar not appearing after delegation setup

I have a UILabel in my ViewController that has a NavigationController (let's say view controller A) with a tap gesture recognizer attached to the label. When the label is tapped another view appears (let's call it B). The user picks some text in B and the view dismisses back to A with the label text updated with the selection. So I created a delegation between A and B to get the selection. The problem is that I do not see the NavigationBar when B appears. Is there a way to fix this?
ViewController A
#IBOutlet weak var sectionName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let sectionLabelTap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
sectionName.isUserInteractionEnabled = true
sectionName.addGestureRecognizer(sectionLabelTap)
}
#objc func labelTapped(_ sender: UITapGestureRecognizer) {
let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
sectionNameVC.selectionNameDelegate = self
sectionNameVC.userData = userData
present(sectionNameVC, animated: true, completion: nil)
}
In order to display the Navigation bar the UIViewController needs to have a UINavigationController.
You can add that sectionNameVC ViewController into a UINavigationController to persevere the present animation.
In that case your code might look something like this:
#objc func labelTapped(_ sender: UITapGestureRecognizer) {
let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
sectionNameVC.selectionNameDelegate = self
sectionNameVC.userData = userData
let naviagtionController = UINavigationController(rootViewController: sectionNameVC)
present(naviagtionController, animated: true, completion: nil)
}
Or you can simply call pushViewController on the View Controller A's navigation Controller, like this:
self.navigationController?.pushViewController(sectionNameVC, animated: true)
This will add sectionNameVC into the View Controller A's navigation Controller stack. In this case the transition animation will be different, the sectionNameVC will come from your right.
You are missing the concept between "Presenting" View Controller & "Navigating" the View Controller. You will get the answer, once you understood the concept. Here, it is..
When you are presenting the ViewController, you are completely replacing the stack container to the new view controller.
STACK holds the addresses of the ViewControllers you push or pop via navigating.
e.g:
present(sectionNameVC, animated: true, completion: nil)
On the other hand, if you are navigating to other view controller by pushing it. In this case, you can go back to previous controller by simple popping the ViewController address from stack.
e.g:
self.navigationController?.pushViewController(sectionNameVC, animated: true)
self.navigationController?.popViewController(animated: true)
So, If you navigate then, only you will get navigation Bar.
Now, in your case, you are presenting the ViewController and hence, navigation bar is not showing.

Setting back button when presenting a view controller

On clicking a button I display a view controller using the following code:
let navController = UINavigationController(rootViewController: locationVC)
navController.navigationBar.barTintColor = StyleHelper.navBarColor()
navController.navigationBar.tintColor = UIColor.whiteColor()
navController.navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "back_arrow"), style: .Plain, target: locationVC, action: nil)
self.presentViewController(navController, animated: true, completion: nil)
But the back button does not appear on the locationVC
What am I doing wrong? Please help
Multiple issues with code.
Issue 1:
You should update viewController's bar button item and not navigationController's bar button item.
So this is wrong
navController.navigationItem.backBarButtonItem
Whats correct
self.navigationItem.backBarButtonItem
Issue 2:
The above code won't work because your current viewController is not pushed by any other VC so it cant show back button. What you need is leftBarButtonItem
So in your VC you can write
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Abcd", style: .done, target: self, action: yourSelectorHere)
}
O/P:
Back button only appears for view controllers in the navigation stack.
you creating new navigation controller stack and making locationVC as root controller so there won't be any view controller to go back.
If you push another view controller to the navigation stack then you will get back button.
In a horizontally regular environment, the view controller is
presented in the style specified by the modalPresentationStyle
property. In a horizontally compact environment, the view controller
is presented full screen by default. doc: presentViewController
So, if you use present a view controller it will not show in navigation controller thus no back button
For this case you need to push view controller.
From storyboard:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier:
"newViewController") as! YourViewController
self.navigationController?.pushViewController(vc, animated: true)
Programmatically
let vc = YourViewController()
self.navigationController?.pushViewController(vc, animated: true)
presenting will present your VC on top of everything and not push it to the navigayionCOntroller.
You should push your VC to the NavigationController instead like so:
var rootViewController = self.window!.rootViewController as UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController
rootViewController.pushToViewController(profileViewController, animated: true)
presentViewController is a method that bring a modal view to your navigation, not part of the navigationController.
Instead you should use the pushToViewController method on the navigationController (and not creating a new one) like that :
if let navigationController = self.navigationController {
navigationController.pushViewController(locationVC, animated: true)
}
The presenting view controller is responsible for dismissing the view controller is presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method.
#IBAction func backButtonAction(_ sender: UIButton) {
self.dismiss(animated: true)
}
More: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss

Combine Seques with UINavigationController

I am building a mobile app which displays stats from multiple APIs.
The app shows different values based on the API that is active. At runtime, the user can select which API to display values for. However, the values are always displayed within the same Target view controller.
UINavigationController is in use and I'd like to defer to that for navigation and navigation UI, if possible, even though UITableViewControllers support navigation bars.
Please see the storyboard design image at the end of the question.
Environment: iOS 9/Swift 2.2/XCode 7
At runtime, the user will select a Choice view controller from the Select controller. What is the appropriate way to segue from one of the Choice view controllers, via a "Done" UIBarButton, to the Target controller? The Target controller does not change.
Must I programmatically define the right bar button to "Done" for every controller that is embedded within the UINavigationController?
class UIViewController: UITableViewController {
override func viewWillAppear( animated: Bool) {
super.viewWillAppear( animated)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
}
}
The proper way to return to the root view controller is:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(MyViewController.done))
}
func done() {
if let navigationController = self.navigationController {
navigationController.popToRootViewController(animated: true)
}
}
}
You do need to programmatically add a "done" button; but it's more simple than that. Just select your root view controller, and copy/past the class in storyboard. Use your segues to push the correct view onto the UINavigation stack.

Back button hidden in navigation bar (but still works)

I have read about similar problems where the problem was caused by having multiple navigation controllers, but I only have one. This is my navigation flow.
VC = UIViewController, NC = UINavigationController
VC1 -modal-> NC -root-> VC2 -show-> VC3
VC1 is not embedded in a navigation controller, I'm starting that modal segue using performSegueWithIdentifier:sender:.
VC2 then is using a show segue to present VC3, which is the one where the back button is not visible. It still works though. But, it does appear if I exit to the home screen and then enters the app again, as shown here:
https://gfycat.com/VelvetyThisHamster.
Any ideas why this is happening?
edit: To make things clear: I want the button both visible and functioning (it's not that it's working that's the problem, but that it's hidden)
EDIT 2:
If I change my navigation flow to this
NC -root-> VC2 -show-> VC3
then the back button works as intended. So the question is, how can I add a regular view controller without a navigation controller before the first navigation controller? I want it before because VC1 should not have a navigation bar and VC2 should be presented modally.
try this
Hidden
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(true, animated: false)
//else use this
self.navigationItem.leftBarButtonItem = nil
}
Show
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
self.navigationController.navigationItem.backBarButtonItem.enabled = TRUE
}
Update
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
let backButton = UIBarButtonItem(title: "leftbutton", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonMethod")
self.navigationItem.leftBarButtonItem = backButton
}
func buttonMethod() {
print("Perform action")
}
I think I found the source of the problem, so I'll post it here in case someone else runs into the same problem.
The modal presentation between VC1 and NC was made from a background queue (by calling performSegueWithIdentifier:sender: in the completion handler of an NSURLSessionDataTask to be precise). By dispatching that line of code to the main queue the problem seems to disappear.
Turn out, I had NavigationBar tint color set to "Clear". Once I changed it, the back button appeared.

Resources