iPhone/iOS: Push Controller onto NavigationController that is currently not selected - iphone

Within my iPhone App I use a UITabBarController that holds four UINavigationController. Each UINavigationController represents a specific navigation within my app.
Normally if I want to push a new controller onto the stack of the current UINavigationController I just do something like:
// push another controller onto the current NavigationController Stack
MyController* controllerToPush = [[MyController alloc]init];
[self.navigationController pushViewController:controllerToPush animated:YES];
[controllerToPush release];
However there are a few places within my app, where the user is located within on navigationController and his action should change the selected Tab of the UITabBarController and push the controller there. I tried something like:
// Get a reference to my appDelegate
MyAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
// Change the selected Index of my TabBarController
[[appDelegate tabBarController] setSelectedIndex:0];
// Get a reference to the NavigationController whose Index within the TabBarController is 0
UINavigationController* navigationController = [appDelegate firstNavigationController];
// Push the newly initiliazed controller onto the navigationController
MyController* controllerToPush = [[MyController alloc]init];
[navigationController pushViewController:controllerToPush animated:YES];
[controllerToPush release];
But this approach doesnt work. The selectedIndex of the TabBar chances correctly, but the controllerToPush has not been added to the navigationController-Stack. What am I doing wrong?

Try doing[appDelegate.firstnavigationcontroller pushViewController:controllerToPush animated:YES];

Related

don't understand how to use navigation controller in iphone

I'm extremly new to iphone and I have the following misunderstanding.
All over internet the tutorials about how to use NavigationController programatically it says:
NavigationController must be declared in applicationDidFinishLaunching and must be init with a root.After that you can add views to it.
I have this:
A UIViewController class meaning(AdiViewController.h, AdiViewController.m and AdiViewController.xib) and no Delegate file meaning no applicationDidFinishLaunching method.
What I wanna do is from my class-AdiViewController when pressing a button to go to another view.
I understand that I need a NavigationController which should retain my views having the root AdiViewController.
But my problem is where should I initializate that NavigationController in viewDidAppear??...cause I don't have the Delegate files.
If you could provide a minimal example with this small issue of mine it would be great.I'm sure that for those how are senior this is nothing but still I don't get it.Thanks
NavigationController must be declared in applicationDidFinishLaunching -> this is not true.
In your AdiViewController if you have button when you push that button you want to load navigation Controller right ?
// Hook this IBAction to your button in AdiViewController
- (IBAction)pushNavController
{
AnotherViewController* rootView = [[AnotherViewController alloc] initWithNibName:#"Anotherview" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[rootView release];
[self presentModalViewController:navController animated:YES];
[navController release];
}
If you are in AnotherViewController i.e., you are in root view controller of Navigation controller. You need to push and pop view controllers from there. For example if you have a button in AnotherViewController:
// push next view controller onto navigation controller's stack
- (IBAction)pushNextViewController
{
NextViewController* nextView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
}
// Similarly if you want to go back to AnotherViewController from NextViewController you just pop that from navigation controller's stack
- (IBAction)pushNextViewController
{
[self.navigationController popViewControllerAnimated:YES];
}

How to call another class on navigation controller and add it to app delegate

I have created a view based application and in the appdelegate .h file I have created UINavigationcontroller object and added it to window subview. Next in the app did finish launching I had allocated and initialised a class that I want to be viewed when the app first launches with the navigation bar on the top of it.
So this is the code I have did to add the class to navigation bar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[window addSubview:navController.view];
//test is the class that i want to be viewed first when the app launches first
test *t = [[test alloc]initWithNibName:#"test" bundle:nil];
[navController.view addSubview:t.view];
[window makeKeyAndVisible];
return YES;
}
but the problem is my test class launches but it does not display the navigation bar. What may be the problem?
Use some like this (add these lines)
appDelegate.h
UINavigationController *navController
#property (nonatomic,retain) UINavigationController *navController;
and in .m
test *t = [[test alloc]initWithNibName:#"test" bundle:nil];
self.navController=[[[UINavigationController alloc] initWithRootViewController:t] autorelease];
[window addSubview:self.navController.view];
[window makeKeyAndVisible];
it helps you.
From other view you need to make object of the appDelegate class then access the navigation controller of that class
see this
yourAppDelegateClass *objAppDelegate=(yourAppDelegateClass *)[[UIApplication sharedApplication] delegate];
now for pushing a view
SecondView *s=[[[SecondView alloc] initWithNibName:#"yournibName" bundle:nil] autorelease];
[objAppDelegate.navController pushViewController:s animated:YES];
this concept help you.
You shouldn't add your own view controller's view as a subview of the navigation controller. This way, you hide the navigation controller's view (including the navigation bar) and it makes the navigation controller pointless, because it doesn't know that your view controller wants to be part of its navigation stack.
Instead, push it on the navigation controller's stack by using:
[navController pushViewController:myViewController animated:NO];
Navigation bar is not coming because you are setting navigation controller's view to your test view. You can do it by setting navigation controller's view controllers which requires an array as -
test *t = [[test alloc]initWithNibName:#"test" bundle:nil];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:t,nil];
[self.navigationController setViewControllers:viewControllers];
First, I don't see where you alloc init your navController. Secondly, you don't add a viewController to a navigation controllers view, rather you push the view controller onto the stack, like this:
[navController pushViewController:t animated:NO];
Hope this helps.

access methods and variables in first navigation controller from pushed view

I normally use UIViews to make my apps - but this one I am using a navigationcontroller. I am pushing a view to the top where I want to add items to an array. However, I cannot access the main navigation controller methods etc. Here's the set up
1) AppDelegate adds navigation controller
[window addSubview:navigationController.view];
2) In RootViewController I push a new UIView
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self presentModalViewController: addNewViewController animated:YES];
3) From the AddNewViewController I then want to access the main RootViewController - but cannot seem to access anything. All the methods etc are declared as I've done it before (using UIViews). This code cannot find anything in the RootViewController.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController myFunction];
I have myFunction in the RootViewController.h file. I have used this method before, but never with a navigation controller. I guess it's something to do with hte stack - but I cannot find out what I've done wrong!
Help is much appreciated!!
* UPDATE *
I have now used
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self.navigationController pushViewController:addNewViewController animated:YES];
to push the view controller. In my code, I am trying to access the tableview to reload it with the following code
[self.navigationController.mainTableView reloadData];
but the mainTableView is not accessible. I have declared and sync'd it but still cannot see it. I also tried to loop through and use the element[0] of the stack (as below) but didn't get anywhere with that either!
NSArray *controllers = [[NSArray alloc] initWithObjects:self.navigationController.viewControllers, nil];
UIViewController *tmpController = [[UIViewController alloc] init];
tmpController = [controllers objectAtIndex:0];
First of all,
[self presentModalViewController: addNewViewController animated:YES];
does something different than
[self.navigationController pushViewController:addNewViewController animated:YES];
The names of those methods should speak for themselves.
And appDelegate.navigationController is not your rootViewController. It is the UINavigationController. You can get an NSArray of viewControllers attached to the navigationcontroller with self.navigationController.viewControllers. Item at index 0 should be your rootViewController.
If you push the viewController on the navigationControllers stack there is no need to use the appdelegate. You can access the navigationController with self.navigationController.

Accessing a Top Navigation Controller from a Subview Navigation Controller

I have a my views and controllers set up like so.
A Tab/Bar controller
Within 1. is a root view controller
within 2. is a programmatically created navigation controller, that is displayed as a subview in the root view controller.
What I am trying to do is access the top tab bar/navigation controller so that i can push a view onto it.
I tried parentViewController but all it did was push the view onto the programmed nav controller.
any suggestions?
This is how i set up my root view controller:
-(void)viewDidAppear:(BOOL)animated{
NSLog(#"ROOT APPEARED");
[super viewDidAppear:animated];
WorklistViewController *worklistController = [[WorklistViewController alloc]initWithNibName:#"WorklistView" bundle:[NSBundle mainBundle]];
UINavigationController *worklistNavController = [[UINavigationController alloc] initWithRootViewController:worklistController];
worklistNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
worklistNavController.view.frame = watchlistView.frame;
[worklistNavController.topViewController viewDidLoad];
[worklistNavController.topViewController viewWillAppear:YES];
[self.view addSubview:worklistNavController.view];
GetAlertRequestViewController *alertsController = [[GetAlertRequestViewController alloc]initWithNibName:#"AlertsView" bundle:[NSBundle mainBundle]];
UINavigationController *alertsNavController = [[UINavigationController alloc] initWithRootViewController:alertsController];
alertsNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
alertsNavController.view.frame = alertsView.frame;
[alertsNavController.topViewController viewDidLoad];
[alertsNavController.topViewController viewWillAppear:YES];
[self.view addSubview:alertsNavController.view];
}
A nested ViewController (ie, inside a view controlled by a ViewController that's actually on the NavController stack) doesn't have direct access to the UINavigationController that its parent's view's controller is a stack member of. That's one MOUTHFUL of a sentence, but the sense of it is: you can't get there from here.
Instead you've got to get at the app's NavController via the App delegate.
YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];
You're using your UIApplication's singleton (contains all sorts of good info about your app), which has a .delegate property pointing to the AppDelegate, and that contains a reference to the NavigationController.
This is how the "Navigation-based Application" Xcode template sets up NavController ownership, anyway. YMMV if you rolled your own--though if you did, you probably wouldn't need to ask this question.
You can use the follow instruccion:
[(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];
It works for me :D
Have a look at UIViewController's navigationController and tabBarController properties. These will return the corresponding navigationController or tabBarController that the given UIViewController 'belongs' to.
So you can do something like:
[customController.navigationController pushViewController:newController animated:YES];
// Similarly for tabBarController ...

pushViewController Not Displaying UIView/Nib with tabbar and nav bar

I'm relatively new to objective c but not programming and am stuck with my iphone app.
I created a nav based app with both a navbar and a tab bar controller. I set the tab bar as the root controller. I'm able to switch between each tab without any issues to various UIViews and UITableViews.
My issue is that in one of my UITableViews that I call from the TabBarController, didSelectRowAtIndexPath function is suppose to display a new UIView. The below code does not give any errors and runs fine but does not show the new Nib.
if(newViewController == nil) {
NSLog(#"yes nil");
BookViewController *aNewViewController = [[BookViewController alloc] initWithNibName:#"BookOptionView" bundle:nil];
self.newViewController = aNewViewController;
[aNewViewController release];
}
BookAppDelegate *delegate = (BookAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.appNavBar pushViewController:newViewController animated:YES];
Now when I do the below, it works fine but it gets rid of the nav and tab which I'm assuming because its a modal call instead of pushing the view controller.
BookViewController *screen = [[BookViewController alloc] initWithNibName:#"BookOptionView" bundle:[NSBundle mainBundle]];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];
Any ideas why I can't get the View Controller to push correctly? In my application delegate file, I declared an AppNavBarController object (inherit from UINavigationController) called appNavBar.
Any help would be appreciated!
If you want to present your view as a modal view with nav controller, you can do it as below:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
[self presentModalViewController:navigationController animated:YES];
Also, from what I see, you have your navcontroller in your appdelegate. So I guess you are using a global navcontroller for all your tab views, which ideally shouldn't be the case. Your navcontroller should be within your tab controller and preferably you need to have different nav controllers in different tabs.
I actually found my answer. I'm not sure I understand why my code above doesn't work but the following accomplishes what I want:
[self.navigationController pushViewController:newControllerName animated:YES];