I have this:
FirstViewController:
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
SecondViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
My problem is that when I comeback from SecondViewController to FirstViewController the NavigationBar is still hidden.Is there a way to make it appear when I'm back in FirstViewController?
In the FirstViewController.m:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}
Yep, it's always possible that a different navigation controller will have set the bar to be hidden. So, in your viewWillAppear set the flag as follows:
self.navigationController.navigationBarHidden = NO;
You need to set [self.navigationController setNavigationBarHidden:NO];
This will do.
Related
I have a tab bar application.
Here's launching code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
self.tabBarController=[[UITabBarController alloc] init];
StartViewController *startViewController=[[StartViewController alloc] initWithNibName:#"StartViewController" bundle:nil];
NavRootViewController *navRootViewController=[[NavRootViewController alloc] initWithNavControllerWithSubViewController:startViewController];
HelpViewController *helpViewController=[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:nil];
SettingsViewController *settingsViewController=[[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
AboutUsViewController *aboutUsViewController=[[AboutUsViewController alloc] initWithNibName:#"AboutUsViewController" bundle:nil];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects: navRootViewController, helpViewController, settingsViewController, aboutUsViewController, nil]];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController=self.tabBarController;
Application launched with 4 tab bar tabs.
This action is called after user presses start button in the first tab's navigation controller's root view controller
-(IBAction)startPressed:(id)sender
{
NSLog(#"startPressed: called");
RootViewController *vController=[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
[self.navigationController pushViewController:vController animated:YES];
}
This works fine but I need to hide tab bar for my RootViewController
property hidesBottomBarWhenPushed does not work.
Help me please, how can it be done?
I hope this helps you:
- (void)viewWillAppear: (BOOL)animated
{
self.hidesBottomBarWhenPushed = YES;
}
Yea you have to add the modalview on window not on the viewcontroller of tabBar.
Try something like.. make an object of AppDelegate like:
AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
then in next line add
[appDelegate.window.rootviewcontroller.view presentModalViewController:vController animated:YES];
or add your code [self presentModalViewController:vController animated:YES] in the viewDidAppear of the firstviewcontroller of tabBar.
What did you do to solve the problem??I would like to know that also.
If you don't want the main view to show the tab bar, you shouldn't be pushing it onto the navigation controller. Doing this causes the application to assume that this new controller is part of the navigation hierarchy. What is probably the best solution is to start your application on the RootViewController, and then present the navigation controller modally. When you're done with the navigation controller, have it call dismissModalViewController on itself.
Solved using this code:
-(IBAction)startPressed:(id)sender
{
NSLog(#"startPressed: called");
RootViewController *vController=[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc] initWithRootViewController:vController];
[vController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[((AppDelegate *)[UIApplication sharedApplication].delegate).tabBarController presentModalViewController:navController animated:YES];
}
Thanks to #iPhone Developer
UIViewController *nextViewController = [[UIViewController alloc] initWithNibName:#"NextViewController" bundle:[NSBundle mainBundle]];
// hide UITabbarController
nextViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
I have a UINavigationController. I load a presentmodalviewcontroller over it. And I push 2 more view controllers over the presentmodalviewcontroller. If I need to move to my first view controller, what should be done?
Edit: I am also loading some UIView over the UIViewController on top of my stack. I have successfully removed that.
I have tried
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
but still its not working
This is how I am adding each view controller
First viewcontroller
FirstViewController *firstViewController =
[[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UINavigationController *navcontrol =[[UINavigationController alloc]initWithRootViewController:firstViewController];
[self presentModalViewController:navcontrol animated:YES];
[navcontrol release];
Second viewcontroller
SecondViewController *secondViewController = [[SecondViewController alloc]init] ;
[self.navigationController pushViewController: secondViewController animated:YES];
[secondViewController release];
Third viewcontroller
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
[self.navigationController pushViewController: thirdViewController animated:YES];
[thirdViewController release];
Ok. I got it.
myAppDelegate *appDelegate = (myAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.navigationController dismissModalViewControllerAnimated:YES];
Is it possible to push a new viewController into an existing view within another view controller?
Here is my setup to begin with:
You can see that I have a UITableView. When a cell is tapped, a new view controller is pushed using this code;
DetailTableViewController *viewcontroller = [[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewcontroller.item = (MWFeedItem *)[itemsToDisplay objectAtIndex:indexPath.row];
[self presentViewController:viewcontroller animated:YES completion:nil];
[viewcontroller release];
Unfortunately this looks like this though and sits on top of the bar at the top saying "Will Roberts"
Why is this happening as it's clearly outside of the view I set it to be pushed from...
Instead of "presentViewController", use
DetailTableViewController *viewcontroller = [[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewcontroller.item = (MWFeedItem *)[itemsToDisplay objectAtIndex:indexPath.row];
[self.navigationController pushViewController:viewcontroller animated:YES];
[viewcontroller release];
or use modal view controller
[self presentModalViewController:viewcontroller animated:YES]
//Presenting new view controller with navigation controller will help you.
DetailTableViewController *viewcontroller = [[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
viewcontroller.item = (MWFeedItem *)[itemsToDisplay objectAtIndex:indexPath.row];
[self.navigationController presentViewController:viewcontroller animated:YES completion:nil];
[viewcontroller release];
//Edited
DetailTableViewController *viewcontroller = [[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:viewController1];
[self presentModalViewController:nav1 animated:YES];
I have a HomeViewController.m in which I push SpecificViewController by self.navigationController,
SpecificViewController *specificViewController= [[SpecificViewController alloc] initWithNibName:#"SpecificViewController" bundle:nil];
[self.navigationController pushViewController:specificViewController animated:YES];
And in SpecificViewController.m I insert a subview
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.view insertSubview:firstViewController.view atIndex:0];
And in FirstViewController.m, there is a tableview. The question is how can I push a new ViewController by navigationController, I have try below, but it does not work.
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailTableViewController *detail =
[[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I still tried a new UINavigationController,
UINavigationController *NEWnavigationController;
NEWnavigationController=[[UINavigationController alloc] init];
[NEWnavigationController pushViewController:detail animated:YES];
but it still did not work. Could you give me any suggestion?
There are quite a few things wrong with your approach.
First, is SpecificViewController the first view controller you load? If yes, then you do not push this view controller rather you set it as the root view controller in your application delegate as shown below
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[SpecificViewController alloc] init];
This will ensure that your Navigation controller is setup and the first view controller that appears is the SpecificViewController
Secondly, you dont insert a view controller as a subview. If you are trying to load a second view controller then you push that onto your navigation controller stack. You can do that from SpecificViewController as shown below
FirstViewController *firstViewController =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.navigationController pushViewController:firstViewController animated:YES];
View *view1 = [[View alloc] init];
[self presentModalViewController:view1 animated:YES];
The code above works when connected to a UIButton. It does not work upon launch of an application by putting it in the viewDidLoad: method. I would like to run this upon launch.
Look very closely at the method you're calling: presentModalViewController: presents a controller, not a view.
The correct pattern is something like this:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentModalViewController:myViewController animated:YES];
[myViewController release];
Since iOS 6 you should use following method, because of deprections:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self presentViewController:myViewController animated:YES completion:nil];