views:

34

answers:

1

Hi guys,

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

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.

fluchtpunkt
ah that makes sense - thanks for the pushviewcontroller code too. I have changed it to that and it feels much better. I have updated my post above with the new code.
Matt Facer