Combine Seques with UINavigationController - ios

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.

Related

In navigation controler how to implement a add button opens a new view controller

I am new to swift , I have a very simple question. I implement a navigation controller with two items at top like this
I did this by adding a navigation controller to the project and then adding this lines of code in to the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: Selector(("barButtonItemClicked:"))), animated: true)
}
now my question is about how to open a new view controller I mean a new page after clicks on plus (+) button at the navigation bar. I searched a lot but did not find any exact thing relate to this. Appreciate you if possible help me. thank you
Your code should be like this one:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, style: .plain, target: self, action: #selector(performToVC))
#objc func performToVC() {
performSegue(withIdentifier: "vc", sender: self)
}
Here is code for Push TO OTherView Controller
#objc func PoushTOHistoryVC() {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "HistoryViewController") as! HistoryViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
here is Button for Add
self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(PoushTOHistoryVC)), animated: true)
you need to add HistoryViewControlle to Stroyboard ViewController HistoryViewControlle and Don't forget
First you have to create new view controller which you have to open on that plus (+) button at the navigation bar. After creating new view controller you have to press and hold the control key on the keyboard and the click on the (+) button at the navigation bar and drag it to the new view controller and then release the click button image for reference, then it will show you a pop up with options such as show, show details etcimage for reference, select show option.

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
}

Swift updating barButtonItems for a view embedded in a tabBarController in a navigationController

Basically I want to set up a barbutton item in my HomeViewController but it's not showing up. I am thinking it might have something to do with my navigation hierarchy. Here is my code for the HomeViewController:
override func viewDidLoad() {
super.viewDidLoad()
backBtn = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("goBack"))
self.navigationItem.setLeftBarButtonItem(backBtn, animated: true)
}
Here is a screenshot of my storyboard. I dynamically push a login screen on didFinishLaunchingWithOptions when not logged in, which is why the UITabBarController is embedded inside the UINavigationController and not the other way around:
Try with following Navigation flow.
You must need to set ViewControllers of UITabbar which are embedded in UINavigationController
Please refer following image for Storyboard.

Programmatically create navigation controller with back button

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.)

Swift Changing Navigation Bar in a ViewController

So here's my problem.
I added a empty ViewController to a NavigationController.
The problem is that when I add a View to the empty ViewController the navigation bar becomes unclickable I can't add back buttons or change the title.
I'm using Xcode 6.1 6A1052d.
You don't add buttons directly to navigation, but to the navigationItem of contained view controller. For example,
class ContainedViewController : UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: nil, action: nil)
}
}

Resources