【发布时间】:2010-02-07 05:23:05
【问题描述】:
我在启动时通过以下代码创建我的 Nav 和 TabBar: 在:myAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;
// create tab bar controller and array to hold the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];
// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
// add the new nav controller (with the root view controller inside it)
// to the array of controllers
[localControllersArray addObject:localNavigationController];
// release since we are done with this for now
[localNavigationController release];
[myViewController release];
// setup the first view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = @"Category1";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image1.png"];
resortsListViewController.navigationItem.title=@"Category1";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the second view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = @"Category2";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image2.png"];
resortsListViewController.navigationItem.title=@"Category2";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the third view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = @"Category3";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image3.png"];
resortsListViewController.navigationItem.title=@"Category3";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[resortsListViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
如您所见,我正在将 ResortsListViewController 重新用于不同的类别显示(带海滩的度假村、带泳池的度假村、带浓缩咖啡吧的度假村)......这是一个测试应用程序)我需要做几件事:
我需要能够知道哪个选项卡单击导致了 ResortsListViewController 的显示。我希望使用 TAG,但“initWithRootViewController”没有“tag”控件。因此,如果我使用作为类别名称的图像文件名,我可以使用该文件名来区分类别......甚至是导航项名称。我需要知道 ResortsListViewController 是否有办法知道哪个选项卡项单击导致它显示。我想寻找一个可以分配给标签栏项目的“动作”,但这不是 tabbarcontroller 的工作方式。
当从一个选项卡单击到另一个选项卡时,视图确实发生了变化,ResortsListViewController 的标题发生了变化,等等……但它所持有的 TABLEVIEW 不会清除并显示任何新数据。在网上搜索我找到了一个可能的解决方案:
http://discussions.apple.com/thread.jspa?threadID=1529769&tstart=0
基本上说:
为了 UINavigationControllers 发送 “viewWill/Did/Appear/Disappear” 消息,它需要已经收到 “viewWill/Did/Appear/Disappear”来自 它的容器。
在这种情况下,我的 UINavigationControllers 的容器是什么? myAppDelegate 在 .h 文件中定义为:
NSObject <UIApplicationDelegate, CLLocationManagerDelegate>
并且没有:
- (void)viewWillAppear:(BOOL)animated {
}
部分。当我添加一个时,它会在调试器中显示“NSObject 可能无法响应 -viewWillAppear”。
有什么帮助吗?
【问题讨论】:
标签: objective-c iphone uinavigationcontroller uitabbarcontroller