This should be very straightforward, however I cannot figure it out which is extremely frustrating.
I have 2 view controllers, each embedded in a navigation controller, and on the 2nd VC I have a button which when pressed should dismiss/pop this view and essentially go back. I have tried both of these and neither works:
[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
Help please?
In Case Using Push/Pop navigation scheme :
Make sure you are embedding the navigation controller properly to the view controller from which your navigation starts.
And also make sure you are using push segue to show the second view controller.
On the other hand : if you are using model segue to present the view controller, no need to embed the navigation controller and this will work
[self dismissViewControllerAnimated:YES completion:nil];
Related
I need to push and pop the ViewController accordingly, but while pressing back button in my application, the ViewController should switch to next ViewController in backward. I. e. normally by pushing ViewController it slides from right to left, but while clicking back, the ViewController should come from left to right.
I normally use simple presenting and push ViewController codes. Please help me. I don't have navigation controller in my application for some pages and some pages have. Please provide me both solutions.
Though your question is not clear, I am assuming you want animation when transitioning back to previous view controller.
If you are not using navigation controller, just dismiss you controller like:
[self dismissViewControllerAnimated:YES completion:nil];
In case of navigation controller use this:
[self.navigationController popViewControllerAnimated:YES];
I have a tab view controller with a navigation controller. In the first tab item I click on a button in a view that pops up a view with animated: YES.
Then when that view is done I hit another button that dismisses it. Like:
[self dismissViewControllerAnimated:NO completion:^{
ProfilesViewController *profile = [[ProfilesViewController alloc] init];
[self.navigationController pushViewController:profile animated:YES];
//SHOW YOUR NEW VIEW CONTROLLER HERE!
}];
But everytime this code runs, it dismisses the view, DOES NOT push the profiles controller, and shows the view from the first tab bar item.
How do I push the ProfilesViewController to the screen with a Back arrow?
If you are using dismissViewControllerAnimated to dismiss that means that the VC is presented modally. As such, it doesn't have a navigation controller (so self.navigationController is nil) and thus it can't push anything into the navigation controller.
You should really add a property to the controller which is a delegate or a completion block which can be used to push the controller from another controller (the one that presents it) to dismiss and push the controller.
A second option is to pass the navigation controller, it's a similar amount of code to using a block but not so good.
A crappy option is to use the parentViewController to find the appropriate navigation controller, but that sucks for many reasons.
this is my problem.
I have a tab bar, in the last tab I load a modal view. When I dismiss the modal view the app returns in the last tab of the tab bar. But instead I'd like that the app returns on the first tab of the tab bar (the initial view).
if I dismiss the modal view the code that I insert after (to call initial view controller) is not taken in account. Can you offer me a solution? Thank you.
The code that I use is:
[self dismissViewControllerAnimated:YES completion:nil];
InitialViewController* controller = (InitialViewController*)[self.storyboard instantiateViewControllerWithIdentifier:#"Initial"];
[self presentViewController:controller animated:NO completion:nil];
What you are doing is not "returning" to the tab bar. Instead, you are presenting a completely new tab bar. Now you have two tab bar interfaces. Don't do that.This is sufficient to dismiss:
[self dismissViewControllerAnimated:YES completion:nil];
To change tabs, you need a reference to the existing tab bar controller (not a different one). Then you can just say:
[theTabBarController setSelectedIndex:0];
If you know for a fact that you want to do that when you return from the modal controller, you can even do that at the time you present the modal controller.
Below is a hierarchy of my navigation controller
MainViewController
|
|
DetailViewController
Then I do the following on DetailViewController
[self presentViewController:reminderController animated:YES completion:nil];
After navigating to ReminderViewController, at some points I do
[self dismissViewControllerAnimated:YES completion:nil];
However, it brings me back to MainViewController instead of DetailViewController
That is weird. Any thoughts about this issue...
EDITED :
The reason I do presentViewController: reminderController animated: completion: on DetailViewController because reminderController is used to send a reminder. Just like goole app or other apps, when sending sth , we are using presentViewController.
Here is a sample of the documentation regarding the UIViewController class:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.
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.
Thus, I think you should first use a segue to push your DetailViewController, and then present the reminderController modally, which you'll be later able to dismiss using dismissViewControllerAnimated:completion: without dismissing DetailViewController.
Instead of using dismissViewController:animated: use
[self.navigationController popViewControllerAnimated:YES];
dismissViewController:animated removes all the UIViewControllers in it.
You will save you a lot of trouble if you read the UIViewController and UINavigationController references. Twice ;)
See this post for more details.
I have a class that is of type UITableViewController.
This class has a member of type UINavigationBar that I use for adding in an edit button for the table view.
Using this method calling is invalid.
[self.navigationController pushViewController:controller];
How can I push a detail view onto the view after selecting a table row without wrapping my UITableViewController in a UINavigationController?
The closest alternative if you don't want to use navigation controller are modal view controllers.
[self presentViewController:controller animated:YES completion:nil];
This will slide the controller into your screen from bottom, or if you change controller's modalTransitionStyle it can flip over or fade in.
To dismiss the controller just call:
[self dismissViewControllerAnimated:YES completion:nil];
I would wrap the UITableView inside a UINavigationController and just hide the UINavigationBar.
[self.navigationController setNavigationBarHidden:YES animated:NO];
And then create a back button that pops the ViewController off the stack.
[self.navigationController popViewControllerAnimated:YES]
What could also be done is to use a Navigation Controller as usual and then hide it.
To hide the Navigation Controller using storyboards: select it and uncheck "Show Navigation Bar" in the attribute inspector. Others might suggest to hide the navigation bar in each controller, but the problem with that is that it will appear for a millisecond and then disappear.
You can't push a view controller onto a navigation controller if there is no navigation controller. If you are wanting to be pushing controllers and have it display the topmost controller and everything, just use a UINavigationController and be done with it.
You can push arbitrary UINavigationItems onto your UINavigationBar, and your bar's delegate will be notified when the user uses the back button so you can take appropriate action. See the documentation for more information.
It's true that without a UINavigationController you can not push view controllers. You rather present view controllers modally via UIViewController.present(_ viewControllerToPresent:, animated:, completion:)
But it's possible to create a custom segue to display the view controller as if it were a push (or any other animation you want), although it seems that using a UINavigationController just makes things easier.
Here are some related links to the documentation:
UINavigationController Class Reference
Customizing the Transition Animations
Presenting a Modal View Controller