ViewController pushing to next according to the specific notation - ios

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];

Related

Dismiss Modally presented view makes tab bar controller (kind of) reset

I have an app which has tab bar controller as main controller. Each tab has a series of views with navigation controller and I normal push and pop those view in stack.
Weird problem is
Case 1 : If I create a UINavigationController and make a new viewController as its root, and present this NavigationController. Within this new navigation stack, I can easily present a view modally and dismiss it without a problem.
Case 2: Now without make a new UINavigationController, I present a view, and when I dismiss a view, the view beneath is behave weirdly. For example, it's the presenting view was UICollectionView, it just scroll back to 1st cell, like it's doing "reload" action and "scrollTo" the first cell. If the presentingView is a pushed view from rootView, it will just popToRoot view, which is definitely not intended.
I didn't have this problem until I implement UITabbarController, so I guess, I should know more that's going on under the hood when presenting a view and dismiss a view in UITabbarController.
I GUESS, when dismiss a view in UITabbarController view, it sort of "RESET" everything to the very first view of it's current tab. I really am not sure it's trure though.
I know it's kind of conceptual, but I can't help to think there must be something critical I am missing here.
I made silly mistake that I sublclass UITabbarController and define navigation controlllers in viewDidAppear instead viewdidLoad, so when I make the window's rootview to tabbar controller, the navigation controllers are not set properly. That's why all punky things happened. It would be nicer if just crash instead of this weird behaviors.
You can try this to go back to your first viewcontroller.
- (IBAction)buttonPressedFromVC2:(UIButton *)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
} // This is going back to VC1.
This method is will be in second viewcontroller.m file. It is button click method.

Neither dismissViewController or popViewController are working

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];

Tab bar when move to other view controller

I have a problem with the tab bar for my app. When I open my app the bottom tab appears since I am using the Tab bar controller however when I move to another view controller and come back the tab bar is gone. Do I have to do something to the storyboard?
Ok, as I suspected, you're going "back" in an incorrect way. You should never go backwards to a previous controller with a segue unless you use an unwind segue. What you're doing is instantiating a new instance of that controller you think you're going back to -- one that isn't embedded in a tab bar controller. So, you either need to use an unwind segue, or go back in code using dismissViewController:animated:completion: (assuming that you're using modal segues to go forward).
If you navigate to another view with the following:
NamedViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"NamedViewController"];
[self.navigationController pushViewController:svc animated:YES];
and use the following to go back it should work.
[[self.navigationController popViewControllerAnimated:YES] viewWillAppear:YES];

Navigate through a button

I have a Master Detail View application. On the detailView I have a welcome screen, and on the tableView I have list of employees. When I click on the employee, call a seperate view on top of DetailView with the below code.
[_dtController.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"OBViewController"] animated:YES];
I can go back from the Navigation bar button, but I have a button on my OBViewController, through which I have to go back. Can anyone tell me how to navigate and go back from a button.
You'll want to take a look at Apple's UINavigationController Class Reference.
Specifically, the below item:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
This can be used from any view within a view to get your desired outcome. It's important to note the difference between "push" and "pop" with regards to the UINavigationController hierarchy. To add a view, you push it to the stack. To remove it, you pop it off the stack.

What exactly does the back button in Navigation controller do?

Is there anyway to tell what specifically the back button from a navigation controller does? I know in terms of what it does, but in terms of method called/functions etc.
The reason is that I am hiding the navigation bar and will have a button on view instead and just want to replicate this completely.
If you want to replicate the back button feature in navigation bar, you need to call the following in that button's action:
[self.navigationController popViewControllerAnimated:YES]
In your view controller call this, when your custom back button is tapped:
[self.navigationController popViewControllerAnimated:YES];
This will pop the current view controller.
This is the UINavigationController method that the back button calls:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
So to replicate it just do something like this in the viewController you are popping...
[self.navigationController popViewControllerAnimated:YES];
You can verify that this is exactly the method called by the back button by subclassing UINavigationController and overriding this method - you will see that it gets called when the back button is pressed (no need to do this in your app - it's just a way for you see what is going on!)
If the you're referring to the leftBarButtonItem and not the built in backButton, then it can do whatever you want. It does not automatically dismiss the current view controller. You need to hook up a selector to the barButtonItem for it to do anything. When that action is fired, you tell it what to do whether it be pop, segue, or virtually anything.

Resources