in the top view of my view controller (the last table view controller) has an add navigation item. i added a view controller object from the objects library and i ctrl + dragged from the plus button to the view controller. i tried the app and it works fine but i can't go back to the previous controller when i reach the last controller. since the last controller connected (by segue) to the plus button, i can't have a navigation bar on top. so i added one and added an navigation item called it Done. i created an IBAction method in the class that the last controller subclasses which have the following code:
[self.navigationController popNavigationControllerAnimated:YES];
However, when i run the app and press the Done button to go back, it doesn't work although i feel like what i did is totally legal.
If you would have made the final view controller segue a Push segue, you'd still have the navigation bar with a back button. It makes sense since you're adding a record that you'd want a modal view.
You can dismiss the current modal view with the following code:
[self dismissViewControllerAnimated:YES completion:nil];
Generally, you should use delegation and dismiss it from the presenting view controller. However, I think it's fine to dismiss yourself if you're using storyboards, segues, and ARC.
Did you create a Bar Button Item and assigned its 'selector' callback?
Related
I am interested in implementing a back button to segue to a previous view controller. I have tried to embed a navigation bar onto the the top of the table view controller but the issue is when I do segue back to the first view controller, by default, Xcode keeps the navigation bar with a back button to go back to the table view controller. Is there a simpler way to implement a segue to go back to the first VC without the navigation bar remaining?
I'm not too sure if this works, but embed your view controllers including the first one inside the navigation controller. That would make all your view controllers with navigation bar above.
On the main view controller (the one you do not want to have the navigation bar), add the line of code inside your viewDidLoad method.
Swift 3:
self.navigationController?.navigationBar.isHidden = true
I found an easy way. On your TableViewController, drag a UIview to the top of the view controller and itll let you insert the view. From there just add your back button
Just assign your "Back" UIBarButtonItem to this method and it should work.
#IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Sounds like a few problems
1) How are you navigating from the first view controller? Is it with Show (Push) if not, you are replacing the first view controller in your stack.
2) Based on your problem, make sure your first view controller is not embedded in a navigation controller
3) As Dylan mentioned, you need to hook your back button to remove the current view controller to return to the first one
4) Extension of (3), how are you segueing back to the first view controller? If you are explicitly navigating to first view controller with your back button handler, it's not actually going back but rather forward and keep the navigation bar.
Here is the scenario: I have 2 views, each embedded in a navigation controller. The first nav bar has 1 edit nav bar item, when tapped it goes to the 2nd VC. The 2nd VC nav bar has a cancel and save button, and when tapped should respond accordingly. I want to code the cancel action first, which will just call [self dismissViewControllerAnimated:YES completion:nil] but it is not doing anything...
I have also tried [self.navigationController popViewControllerAnimated:YES] and neither works, please help
BTW both segues are push segues
sounds like something off in you segues ...
i did a quick test in the following way:
create two view controllers in the story board ,each has it own navigation controller.
crate a segue from the first view controller to the second one.
2 in the first view controller i added a bar button that preform the segue from 2.
3 to the second view controller i added a bar button with an action that did : popViewControllerAnimated:YES..
all works fine...
though i don't really understand why you need 2 navigation controllers here....
I have an iOS app with 3 views. I am using a "push segue" to move between them. I have linked a push segue from a button in First view to Second View. For moving from second to third, I cannot have a button. I have a list of items and the person can click in any of those to move to third(and last) view. So I added a segue in storyboard and calls it programmatically. The third view is opening properly except that the nav-bar is no longer there.
[self performSegueWithIdentifier: #"moveToFilesSegue" sender: self];
I can live without nav-bar. But the real problem is that I cannot do a push segue from my third view to any other view.
So my question is whether there is a way to call a segue from second view to third view in such a way that the third view also has the navigation controller that the first two have by default(ie navbar is also visible in third segue). There is some break in navigation because of me calling a segue programmatically.
So a few comments. If you would like to be able to get to the two other views from any view at any point in time, then I would suggest using a tab bar controller rather than a navigation controller.
If you must use a navigation controller, then you should keep the first view controller as the root view controller. Imagine then tapping a button that pushes the second view controller. Then say you tap a button to get to the third view controller from the second. What you should really do is perform an unwind segue from the second and then immediately perform a segue to the third. The animations will be a bit undesirable by default since it will probably show the pop and the push, but to treat it like a tab bar, you could just disable the animations on the pop and push.
To implement, keep an int variable in the first view controller (btw.. you can do this by keeping the int in the navigation controller as well if you want). In the viewDidAppear, check the int variable. If it == 1 then push the second view controller. If it == 2, then push the third view controller. Else, don't do anything. When a button is pushed in the second view controller to view the third, unwind the second view controller, but in prepareForSegue set the int variable in the destination view controller to 2 . Thus, when the first view controller appears, it will immediately push the third view controller.
I know it is a bit annoying, but you don't really want to keep pushing the same view controllers over and over again without unwinding.
I have a TabBarApplication with four views in the main TabBarItem. The problem comes when I go to any of these views and click in any button to go to another view and when I go back by a button linked to the main view, the TabBarItem of the app disappear!!
For example, one view of the app is a tableView in which each element of the list is linked to his external view and it has a back button that should return to the tableView. All the segues are by modal, not push because push segue crash the application and by modal it runs correctly but the problem comes when I returned by clicking the back button of the NavigationItem in the header of the view to his main view and the TabBarItem of the app is not there, is empty.
Each tab should have the view controller set to a navigation controller, with the view controller you want set as the root view controller of the navigation controller. Now you can use push segues and the standard back button that will be added for you. This will bypass the issue (and work much better for you and users).
You current issue is likely related to not really ever going back. Instead, just always presenting new modal view controllers which replace any existing content on screen.
I have an initial 'parent menu' view controller with a 'photos' button (more buttons to come like 'events').
From the Photos button I have a simple ctrl-drag segue to a navigation controller which has a tableview as it's first view(albums), collections view(thumbs), then imageview(fullsize).
The problem I am having is:
I want to be able to segue back to the initial simple 'parent menu' view controller from the first table view after the nav controller.
I tried:
dragging a button into the table view's top menu bar area (it was considered a UINavigationItem) and dragging a segue back to the menu... didn't work.
setup outlet from button in the tableview menu area to its parent view and calling [self performSegueWithIdentifier: #"BackToMenu" sender: self]; from there, didn't work.
dragging a button into the table view as a last item and ctrl+dragging it as a segue to the menu: didn't work.
tried a segue going back from the nav controller to the menu and manually calling it, but the outlet function connected to the 'menu' button I dragged into the tableview top back NEVER gets hit.
What am I missing?
I plan to have a few navigation controllers going out from the menu page in the storyboard from multiple buttons.
You don't need a segue for this. You can create a button in your table view and link it to an action in your table view which dismisses the currently presented view controller (I'm assuming your segue that presented the navigation controller stack in the first place was of a "modal" type):
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
Alternatively, you can look into "Unwind Segues" but I haven't used these myself yet so wouldn't like to include details here.
You can drag a "Bar Button Item" to the left side of the first table view after the navigation controller. From there, do the control-drag back from the bar button item to the initial view controller, and choose a "modal" segue. While this answers your question, as #jrturton said in the comments below, this isn't optimized, because it will create a new instance.
If you want to do this programmatically (which will return to the existing instance), create an IBAction for the Bar Button Item on the table view controller. For example in iOS 6:
-(IBAction)returnToParentMenu:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
or in iOS 5:
-(IBAction)returnToParentMenu:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
What I wound up doing was making the nav controller first before anything and then my menu page was first with buttons down each respective path.
on the above: The ctrl+drag didnt work from the nav bar button, kept totally blowing up.
I did not realize I could go from nav controller to a regular view controller first. thought it needed to go to table view first since when you drag out a nav it comes paired with a table.
If I had 10+ 'points' in this strange forum I'd post a screen shot of the storyboard.
Thanks all