我有一个基于 NavigationController 的 iPhone 应用程序,它有一个导航栏和一个工具栏。这基本上是它的工作原理:
applicationDelegate 将“SplashScreen”作为模式视图推送到 RootViewController。当启动画面启动时,应用程序会做一些工作,并且基于用户的位置将关闭模态视图,或者将关闭模态视图并将另一个视图推送到导航堆栈上。
RootViewController 和子视图都有带有添加按钮的工具栏。我的问题是:当第二个视图被自动推送时,添加按钮调用其父控制器的代码。如果您关闭它然后再次按下添加按钮,它会调用正确的代码。
这是我的一些代码。
在 RootViewController 的 viewDidLoad 中,我有:
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked:)];
[self setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
在子控制器(LocationListsController)的viewDidLoad中我有:
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked:)];
[self setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
(是的,相同的代码,它们都有 addClicked 事件)
在 RootViewController 中,viewWillAppear 是我实际推送子视图的位置:
if (((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex != -1)
{
GeoLocation *location = [((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).locations objectAtIndex:((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex];
((GeoListsAppDelegate *)[[UIApplication sharedApplication] delegate]).selectedIndex = -1;
if (lController == nil)
{
LocationListsController *aController = [[LocationListsController alloc] initWithLocation:location];
self.lController = aController;
[aController release];
}
[[self navigationController] pushViewController:lController animated:YES];
}
视图的推送工作正常。我唯一的问题是工具栏上的 addButton 。有没有人有任何想法?