I am using UITabbarController which has three child UIViewController named as FirstViewController, SecondViewController, ThirdViewController. I have a button on FirstViewController. When we tap the button, it need to push navigate to FourthViewController. Here the FourthViewController navigate inside UITabBarController itself. But i need it navigate out of UITabBarController not inside.
Button Target Action
ForthViewController *share = [[ForthViewController alloc] init];
[self.navigationController pushViewController:share animated:YES];
Could anyone guide me for right way. Thanks in Advance..
Try this:
ForthViewController *share = [[ForthViewController alloc] init];
[self.parentViewController.navigationController pushViewController:share animated:YES];
As long as your tab bar controller is in a navigation controller, this will push your view controller on that navigation stack.
Related
In my application i am launching one screen using present UIModalViewController and on that screen I have oneUIButton, if we click on that UIButton alert will come then select yes on alert view now we have to call another view usingpushviewcontroller. But screen is not coming if we use below code can any one help me.
[self.navigationController pushViewController:requestViewController animated:YES];
Try with one root navigation controller and then present your controller modally as follows :
FirstViewController *firstView=[[FirstViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstView];
[self presentViewController:navigationController animated:YES completion:nil];
and then for push another view as follow :
SecondViewController *secondView=[[SecondViewController alloc]init];
[self.navigationController pushViewController:secondView animated:YES];
It will work first using present modal viewController and then using push navigation viewControllers on to the stack.
For my code structure:
On AppDelegate, I declared 4 UINavigationController with their own root UIViewController for my UITabBar.
I created one custom UIViewController as template, in where my other UIViewControllers are sub-class.
On my template:
I have my rightBarButtonItem to show current user profile.
// public method added on template
- (void) goToProfile {
NSLog(#"going through...");
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
}
For my leftBarButtonItem:
- (void) goBack {
[self.navigationController popViewControllerAnimated:YES];
}
First click on the rightBarButtonItem, works fine. If I click the leftBarButtonItem to go back then re-click the rightBarButtonItem, it won't work anymore.
In addition, I have a button on one of my UIViewController that is calling the public method goToProfile. And that works fine.
I got help from my co-worker. The approach is something similar to #Vidhyanand900 answer. I hope this will help others in the future.
tabbarSelectedIndex = 1; // profile tab index
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
UINavigationController *navController = [_appDelegate.mainTabBarController.viewControllers objectAtIndex:tabbarSelectedIndex];
[navController pushViewController:ctrl animated:YES];
self.mainTabBarController.selectedIndex = tabbarSelectedIndex;
If you are pushing or popping with UINavigation controllers of UITabBar
Else show UINavigation Controllers inside UITab Bar..i.e; Is Profile View controller is part of UITab bar or not..
Then you need to change the index of tab bar as below
self.tabBarController.selectedIndex=0;//if profile is first tab
ProfileViewController *ctrl = [[ProfileViewController alloc] init];
[self.navigationController pushViewController:ctrl animated:YES];
Hope it helps you...
I am trying to present a UINavigationController modally from a UIViewController. From previous experience, I'm pretty sure that when I do this there should be a cancel button by default in the navigation bar, however with the following code the navigation bar is completely blank. Any ideas?
UIViewController *rootVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootVC];
[self presentViewController:navController animated:YES completion:nil];
You will not get any bar button items by default. You need to either add the button before presenting, or, more correctly, add the button from inside the root view controller. Normally, you want to have delegation set up, where the presented view controller notifies the presenting view controller that it should dismiss it. Self-dismissing from inside the root view controller is usually not recommended.
– presentViewController:animated:completion: only going to present your view controller as a modal view controller, even you set up that as a navigation controller. the easiest solution in your case is in the navController set the left bar button as the cancel button, and call dismiss when click that button,
UIBarButtonItem *cancelButton= [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissViewController)];
navController.navigationItem.leftBarButtonItem = cancelButton;
- (void)dismissViewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope that would help.
How do I add the back button to a navigation bar? I know there are some related questions but none of them helped. I'm not using storyboard nor did I start with the Master Detail template. Is there a setting to do this? or do you add it by code? if so what code do I need? Any help is appreciated! THANKS!
The back button will appear by default if you use a navigationController correctly. The ViewControllers should be this
NavigationController > FirstViewController > SecondViewController
You will need to create navigationController and instantiate with firstVC as the root. From firstVC you can push secondVC and the back button will be there.
The following placed into appDelegate application didFinishLaunchingWithOptions: will load firstVC initially. Place this after you initialize the window...
UIViewController *firstVC = [[UIViewController alloc] initWithNibName:#"firstVC" bundle:nil];
// any setupup of firstVC
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self.window setRootViewController:navCon];
Then in your firstVC class if you want to push secondVC
in firstVC.m:
UIViewController *secondVC = [[UIViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
You don't need to explicitly add a back button. The button will be added automatically when you push controllers into a UINavigationController. That is, a call as:
[navigator pushViewController: controller animated: ...];
creates a back button.
I have UIViewController with tabbar botton, and navigation bar and navigation item,
when I press on my button in navigation Item I want to load a view, I don't know how to load this view on tabbar,
would you please help me,thanks in advance!
-(IBAction) infoPage:(id)sender
{
InfoCtrol *i = [[InfoCtrol alloc] initWithNibName:#"InfoCtrol" bundle:nil];
[self.navigationController pushViewController:i animated:YES];
}
This will load the InfoCtrol controller and set the Touch UpInside Event in xib to the infoPage method
Do you mean you want to hide the tab bar when you push new controller in navigation?
If true. There is a property hidesBottomBarWhenPushed in the UIViewController class.
// Instead of adding ViewController to TabbarController, add NavigationControllers.
// Eg.
UINavigationController *NavController = [[UINavigationController alloc] initWithRootViewController:_viewCtrl1];
[watchListNavController.navigationBar setTintColor:[UIColor blackColor]];
_tabC = [[UITabBarController alloc] init];
_tabCt.viewControllers = [NSArray arrayWithObjects:NavController,_viewCtrl2,_viewCtrl3, nil];
// Now you can use Push and Pop In your _viewCtrl1.
// Do same with all the viewController