开发者

access methods and variables in first navigation controller from pushed view

开发者 https://www.devze.com 2023-01-19 04:45 出处:网络
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 navigat

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];   
开发者_Go百科

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消