I created a table view and from there let say a user pressed a cell it will go to ListTavleView but the only problem right now is that whenever a user is in ListTableView there is not back button even thought i already embed a navigation controller
and i want the fist view navigation bar is small title second view navigation bar is large title
enter image description here
Below is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
controller.item = items
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
Below is my storybord setup
Navigation bar with no back button
From the image it seems that view controller is added as a child view controller in current view controller.
There is not need to embedded navigation controller when a cell is pressed becoz there is already a navigation controller at start point so no need to create a new one.(If you present a view controller then you may need to embed navigation controller.)
So the solution is...
Delete the navigation controller.
Connect directly to the destination view controller without navigation controller as there is already.
it is better if you use pushViewController, just get a reference of the other view controller, it will always a back button since you are pushing threw navigation Controller here is a simple example:
let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
self.navigationController?.pushViewController(vc, animated: true)
as for the back button, the issue is with your hierarchy.
are you changing the left item of navigation bar in another view controller that might affect navigation bar in your destination view controller.
You are pushing new NavigationController(say Nav.B) to the existing one(Nav.A).
Each navigation controller keeps different navigation stack. The back button is visible when you add viewcontroller to Navigation controller. Read more about UINavigationController.
For your current scenario, you could delete the second navigation controller(i think it not essential) & connect direct segue to ListTableViewController
So this
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
becomes
let controller = segue.destination as! ListTableViewController
When you need large titles(available 11+), you can add this line in viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
And if it needed only for this Viewcontroller, add in viewWillDisappear() or viewDidDisappear()
navigationController?.navigationBar.prefersLargeTitles = false
If you wanted to have navigation bar back button on next view, then just push the target view on navigation, it will show default navigation back button. No, need to any extra work.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
guard let controller = segue.destination as? ListTableViewController else {
return
}
controller.item = items
self.navigationController?.pushViewController(controller, animated: true)
}
}}
And if you are pushing the viewcontroller with segue, then no need to add below line self.navigationController?.pushViewController(controller, animated: true)
Related
I have a popover view which is simply a stack of UIButtons.
The popover is presented from a view controller (Records) which is itself inside a NavigationController.
I need the buttons in popover view to be able to push other views on top of the navigation stack.
Here's how I prepare the segue for the popover in the Records view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popoverSegue" {
let dest = segue.destination as! PopoverViewController
dest.navController = navigationController
dest.modalPresentationStyle = .popover
dest.popoverPresentationController?.barButtonItem = addButton
dest.popoverPresentationController?.delegate = self
}
}
Then inside the popoverViewController I got a bunch of IBAction functions where I need to push other views on top of the navController that was set above.
let editor = EditorViewController(nibName: "EditorViewController", bundle: nil)
navController?.pushViewController(editor, animated: true)
This kina works and the editor view shows up with a nav bar and all, but as soon as I tap on the view or try to scroll, it just gets dismissed.
How can I prevent that dismiss thing? I did try setting isModalInPresentation. It didn't work for me.
Answering, as per OP's comments...
The proper approach is to have your "popover" controller tell the presenting controller to push a new VC onto the navigation stack.
This can be done in a few different ways, but most commonly by using either the protocol/delegate pattern or with closures.
In my iOS app, in a viewController I try to open a specific view in Tab Bar Controller.
I use a performSegue.
First I try to navigate straight to the specific view , but in this case the tab bar disappear
So I try to navigate to the tabViewController, and this lead me to the defult view (the first)
any idea how to navigate to a specific view in TabBarController ?
the performSegue I use:
self.performSegue(withIdentifier: "goToMain", sender: self)
//"goToMain" is my indentipier to the storyboard Segue
I use swift 4
with this code, you don't need segues, you can use when you push some button
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! tabBarLoginViewController
VC1.selectedIndex = 2 //this line says that the view that appears will be third of you tab bar controller
self.navigationController!.pushViewController(VC1, animated: true)
if you want to use segue use this, the segue needs to point of tab bar controller, not a view of tab bar controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToMain") {
let vc = segue.destination as! TabBarController
vc.selectedIndex = 2
}
}
I have a view controller connected to a popover controller, which directs to a new view controller. I want to add a back button on the navigation item to the last view controller so that when I click back it will return to the first view controller
I tried creating segue and click action in the popover controller and add back button in the segue function/click action function
neither of them works
This is what I do in the Popover. I already present the popover with UIPopoverPresentationControllerDelegate and this is what PopoverVC looks like. In this way, I add IBAction to enable the click action of the label in popover. I write the backbutton in the IBAction function and create the navigationcontroller with rootview, and the back button doesn't appear.
class PopoverViewController: UIViewController {
//TODO: add BACK button
#IBAction func createNewChat(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle:nil)
let contactVC = storyboard.instantiateViewController(withIdentifier: "contacts") as? NewContactsViewController
let nc2 = UINavigationController()
let back = UIBarButtonItem()
back.title = "Back"
nc2.navigationItem.backBarButtonItem = back
nc2.pushViewController(contactVC!, animated: true)
self.present(nc2, animated: true, completion: nil)
}
}
I also tried another way, I create a segue in storyboard and connect the popover to the navigation controller, the backbutton still doesn't appear.The segue is hooked up to the navigation controller in the storyboard.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let seg = segue.destination as! ContactsTableViewController
let back = UIBarButtonItem()
back.title = "Back"
navigationItem.backBarButtonItem = back
}
My question is: how to make the back button appear? After it appears, how to make it work? I don't want the back button points to the popover, I want the back button will make users return to the VC that creates the popoverVC.
The back button in a navigation bar doesn't belong to THIS view controller; it is the back button item of the previous view controller already on the navigation stack. But you have no previous view controller. So just use, like, a left bar button item that says Back, or something.
Thus, give the bar button item (back) an action and target and change
nc2.navigationItem.backBarButtonItem = back
to
nc2.navigationItem.leftBarButtonItem = back
I have a Main VC which has a connection to Tab Bar Controller via segue and that Tab Bar controller has 2 sections and those are 2 ViewControllers. I need to pass data from Main VC to that specific VC which derived from Tab Bar Controller using segue. Since i am able to use only one segue (to show only TBController), is there any way to achieve to pass data directly from main to another by skipping Tab Bar controller?
Ps. Those 2 VC have a navigation controller connection between Tab Bar Controller and themselves as well.
Try This
var secondTab = self.tabBarController?.viewControllers[1] as SecondViewController
secondTab.array = firstArray
On prepare for segue function you can get the tabBar associated controllers via TabBarControllers property viewControllers - that let you select one of a tab bar controller's view controllers to become the active view controller. then you use tabBarController's property selectedIndex to actually segue to that VC.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let index = 0//the index of the tab you wish to segue to
if let bar = segue.destination as? TabBarVC {
if let nav = bar.viewControllers[index] as? UINavigationController {
if let navChildVC = navigationController.viewControllers[0] as? YourVCType { // index of the controller of enumerate to find it
// pass data to navChildVC
bar.selectedIndex = index
}
}
}
}
I just ported my project over to Swift 2, and everything is working great - except that even the most simple segues have no back button. Here is the prepare for segue function that I am using:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if segue.identifier == "showExercise" {
if let nav = segue.destinationViewController as? UINavigationController {
if let exercisesController = nav.topViewController as? ExercisesController {
let cell = sender as! WorkoutCell
if let workout = cell.name!.text {
exercisesController.exercises = Workouts[workout]!
exercisesController.navigationItem.title = workout
}
}
}
}
}
Before, the back button to the parent segue used to automatically populate. Now, all I get is the title in the child navigation vc
Are you using show or show detail segue? It seems like you are using a modal segue. Destination view controller for show or show segue is usually the second view controller itself, and not embedded in another UINavigationController.
If your destination view controller for the show segue is really a UINavigationController, the new navigation controller's navigation bar settings may override the old one (the navigation controller of the source view controller). Try not embedding your destination view controller in another UINavigationController.