access methods and variables in first navigation controller from pushed view - iphone

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.

Related

How to pop the split view controller "DetailViewController?"

detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil];
[self.splitViewController.splitViewController viewWillDisappear:YES];
detailViewController.strDetailId = [teaserSectionOneArray objectAtIndex:indexValue] ;
NSMutableArray *viewControllerArray = [[NSMutableArray alloc] initWithArray:[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeAllObjects];
[viewControllerArray addObject:detailViewController];
[[self.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
[self.splitViewController.splitViewController viewWillAppear:YES];
[viewControllerArray release];
This code using for push to the detail view. How do I pop to another view controller in detail view when I click the button? Its not supported the [self.navigationController popViewControllerAnimated:YES];. How to handle this problem? Thanks in advance!
Wooooow thats NOT how you push a view controller on the stack of a navigationController !!
You don't have to call viewWillDisappear, viewWillAppear and such yourself ! You don't have to add the detailViewController to the splitViewController.viewControllers array yourself either!
What you need to do is this:
Make sure your UISplitViewController has a NavigationController as part of its detailViewController (namely the ViewController that is on the right of the screen should be an UINavigationController, not a regular UIViewController).
The rest is then easy as it works the same way as any regular UINavigationController. The fact that your UINavigationController is the right part of an UISplitViewController is not different than when you use an UINavigationController in any other context.
Simply use pushViewController:animated: and popViewControllerAnimated: methods of UINavigationController to push and pop your view controllers. Access your UINavigationController from your UISplitViewController using either a custom IBOutlet that you added to point to it, or by accessing (UINavigationController*)[self.splitViewController.viewControllers objectAtIndex:1].

Return to the first index of UINavigationController?

I'm doing an application which uses a UINavigationController and I'm switching to other UIViewControllers as follows:
if(self.myViewController == nil){
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
self.myViewController = aViewController;
[aViewController release];
}
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myNavController pushViewController:myViewController animated:YES];
I imagine this is creating a pile of UIViewControllers into the UINavigationController, maybe an array of indexs? I would like to know how to turn back without having to be back one by one.
For example, I'm sailing through a few screens and with a button I would like to return at the first index of navigation. I would also like know how to modify indexes, view, erase and anything pertaining to this issue.
Sorry if I have not explained well.
You've asked two questions.
The first is, how do I get back to my first view controller. As #Thomas Clayson and #ender have answered, you want the popToRootViewControllerAnimated: method of your navigationcontroller object for that.
The second is how to move to a particular index in the view controller stack. The answer to that is, you can set the array of viewControllers explicitly. So you can pull out the current listing of view controllers, modify it, and set it back into the navigationController stack. It'll reset the stack and animate you moving to the top item in the stack.
Thusly:
NSMutableArray *controllers = self.navigationController.viewControllers;
[controllers removeObjectAtIndex:[controllers count] - 1]; //or whatever
[self.navigationController setViewControllers:controllers animated:YES];
NSArray *viewControllers = [[self navigationController] viewControllers];
for (int i = 0; i < [viewContrlls count]; i++){
id obj = [viewControllers objectAtIndex:i];
if ([obj isKindOfClass:[yourViewControllername class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
Using this you can come back to any specified viewController.
[self.navigationController popToRootViewControllerAnimated:YES];
Will take you back to the very first view controller (root view controller).
Hope this helps
Use this
NSArray *viewContrlls=[[NSArray alloc] initWithArray:[[self navigationController] viewControllers]];
id obj=[viewContrlls objectAtIndex:1];
[[self navigationController] popToViewController:obj animated:YES];
[viewContrlls release];
You should use popToRootViewControllerAnimated: From UINavigationController class reference:
Pops all the view controllers on the stack except the root view
controller and updates the display.
You can return to the first view with
[self.navigationController popToRootViewControllerAnimated:YES];
That being said, you can also remove a particular view controller, or navigate to a specific index in your view controller if you look at the example.
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack
[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this.
navigationController.viewControllers = allViewControllers;

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

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

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