从推送视图访问第一个导航控制器中的方法和变量
我通常使用 UIViews 来制作我的应用程序 - 但这个我使用导航控制器。我将视图推到顶部,我想将项目添加到数组中。但是,我无法访问主导航控制器方法等。这是设置
1)AppDelegate 添加导航控制器
[window addSubview:navigationController.view];
2)在 RootViewController 中我推送一个新的 UIView
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self presentModalViewController: addNewViewController animated:YES];
3)然后从 AddNewViewController 我想访问主 RootViewController - 但似乎无法访问任何事物。所有方法等都按照我之前所做的那样声明(使用 UIViews)。此代码在 RootViewController 中找不到任何内容。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController myFunction];
我的 RootViewController.h 文件中有 myFunction 。我以前使用过这种方法,但从未使用过导航控制器。我想这与 hte 堆栈有关 - 但我无法找出我做错了什么!
非常感谢帮助!
* 更新*
我现在已经习惯了
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self.navigationController pushViewController:addNewViewController animated:YES];
推送视图控制器。在我的代码中,我尝试访问 tableview 以使用以下代码重新加载它
[self.navigationController.mainTableView reloadData];
,但 mainTableView 不可访问。我已经声明并同步了它,但仍然看不到它。我还尝试循环并使用堆栈的 element[0] (如下所示),但也没有得到任何结果!
NSArray *controllers = [[NSArray alloc] initWithObjects:self.navigationController.viewControllers, nil];
UIViewController *tmpController = [[UIViewController alloc] init];
tmpController = [controllers objectAtIndex:0];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
做一些与
这些方法不同的事情。这些方法的名称应该不言而喻。
并且 appDelegate.navigationController 不是您的 rootViewController。它是 UINavigationController。您可以使用 self.navigationController.viewControllers 获取附加到导航控制器的 NSArray viewControllers。索引 0 处的项目应该是您的 rootViewController。
如果将 viewController 推送到 navigationControllers 堆栈上,则无需使用 appdelegate。您可以使用 self.navigationController 访问 navigationController。
First of all,
does something different than
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.