Navigate through a button - ios

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.

Related

ViewController pushing to next according to the specific notation

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

Bypass UIView without showing

I am using Storyboards for an iOS 7 App. The root controller is a menu. For almost every view, I have a Menu button which brings the App back to that menu using [self.navigationController popToRootController:TRUE]. However, there are two UIView elements where I would like to clear the Navigation Controller view list (as happens when you pop back to the root controller), but then immediately go to another UIView without having the user see the root controller's view. Once at this new view, if the user presses the back button, I want them to go to the menu view.
I've tried to put a performSegue in the viewWillAppear, but it really messes up the Navigation Controller and views. I've tried putting a performSegue in the viewDidAppear, but the user sees first the Menu view flash in, then out on it's way to the correct view.
I hope I've explained this well enough. I hope this can be done.
Thanks.
Your best bet is to build the navigation controller stack yourself, and then use - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated: to replace the current stack.
So you could do something like
...
UIViewController *vcToPush = ...;
NSArray *newVCStack = #[[self.navigationController.viewControllers firstObject], vcToPush];
[self.navigationController setViewControllers:newVCStack animated:YES];
This will add your new controller to the stack using the standard push animation (or not if you so choose), and after the animation is complete, set the view stack to that of the array.

Pushing view controller onto view control in tab controller

So I have a tab bar controller that holds a search view and a profile view. When I click on one of the cells in the search view I want to go to another view controller, still have my tab s on the bottom and maintain the user's ability to click a back button to go back to the main view.
I've achieved the back button part, but I haven't achieved the maintaing tabs part.
This is what I've tried -
-(void)displayCardController{
if(self.userProfile == nil){
[self.tabBarController setViewControllers:#[self.searchViewController, self.loginViewController]];
[self.searchViewController.navigationController pushViewController:self.searchViewController.detailController animated:YES];
} else {
[self.tabBarController setViewControllers:#[self.searchViewController, self.profileViewController]];
[self.searchViewController.navigationController pushViewController:self.searchViewController.detailController animated:YES];
}
}
The idea is - set the tab to have my controllers, and then push what I want to be on top. That doesn't work.
How do I achieve this?
It looks like the problem is that your first tab controller child, self.searchViewController, has a navigation controller. If you want to be able to push onto this controller while still staying inside the tab controller, you need the search view controller (or whatever is the first tab controller child) to be a navigation controller.
Note that its navigation bar can be hidden, so it won't look like a navigation controller, but when you push, you can show the nav bar and so give the user a clear way to get back.
Alternatively, use a different interface. What I do, for example, when I have two tabs and one of them needs to change temporarily, on the iPhone, is use a presented view controller: instead of push/pop, I use use present/dismiss. On the iPhone, this hides the tab bar, but we return to the same place when we're done so the interface is clear. (On the iPad, a presented view inside a tab bar controller does not have to hide the tab bar.)

UIBarButton does not go back to Menu

This may be a simple answer, but I am new to this... my back button in the navigation bar is confused and does not want to go back to the Main Menu. Instead, it goes back to the previous view. How can I specify in my app that the "back" button should always go to to a specific view?
All you have to do is create a UIBarButton and connect an outlet to it. In the action:
-(IBAction)MainMenu
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
If you want to go back to the root controller:
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to go back to an specific controller:
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
You will have to create a new outlet in your view controller to handle the button press. Then use the code NSElvis posted in his answer to pop to root.

How to load a view which has a back button option

I'm trying to recreate the way Notes(native) shows the back button for accounts. In my project I want to load a view by using the tab bar on the bottom, and that view to have a back button pointing to another view just like in Notes. How can I achieve that?
you have to put your UIViewController into a UINavigationController and then present that NavigationController containing your UIViewController. You should have a Navigationbar at the top of your View. If not, in your UIViewController, do:
[self.navigationcontroller setNavigationBarHidden:NO];
hope that helps

Resources