当“以编程方式”时创建 UINavigationController 和 UITabBarController,如何处理它们的功能(如 viewWillAppear?)
我在启动时通过以下代码创建导航和 TabBar: IN : 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。我希望使用标签,但“initWithRootViewController”没有“标签”控件。因此,如果我使用图像文件名作为类别名称,我可以使用该文件名来区分类别...甚至是导航项名称。我需要知道 ResortsListViewController 是否有办法知道哪个选项卡栏项目单击导致其显示。我想寻找一个可以分配给选项卡栏项目的“操作”,但这不是选项卡控制器的工作方式。
当从一个选项卡单击到另一个选项卡时,视图确实会发生变化,ResortsListViewController 的标题也会发生变化,等等...但它所保存的 TABLEVIEW 不会清除并显示任何新数据。在网上搜索我找到了一个可能的解决方案:
http://discussions。 apple.com/thread.jspa?threadID=1529769&tstart=0
基本上是这样说的:
为了 UINavigationControllers 发送 “查看/是否/出现/消失” 消息,它需要收到 “viewWill/Did/出现/消失”来自 它的容器。
在这种情况下,我的 UINavigationControllers 的容器是什么? myAppDelegate 在 .h 文件中定义为:
NSObject <UIApplicationDelegate, CLLocationManagerDelegate>
并且没有:
- (void)viewWillAppear:(BOOL)animated {
}
部分。当我添加一个时,它在调试器中显示“NSObject 可能不会响应 -viewWillAppear”。
有什么帮助吗?
I am creating my Nav and TabBar in code at launch via:
IN : 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];
}
As you see, I am re-using ResortsListViewController for different category displays (resorts with Beaches, resorts with Pools, resorts with espresso bars) ... now, without harassing me (grin) about the silliness of my categories (cos this is a test app) I need need to do several things:
I need to be able to know which tab click caused the ResortsListViewController to be displayed. I was hoping to use TAG but "initWithRootViewController" does not have the "tag" control. So, if i use an imagefilename that is the category name, I can use that filename to distinguish categories...or even navigationItem name. I need to know if there is a way for ResortsListViewController to know which tabbar item click caused it's display. I thought to look for a "action" that I could assign to the tabbar item, but that is not the way tabbarcontroller works.
When clicking from one tab to another, the view does indeed change, the title of ResortsListViewController changes, etc...but the TABLEVIEW it holds does not clear and display any new data. Searching the web I have found a possible solution:
http://discussions.apple.com/thread.jspa?threadID=1529769&tstart=0
basically saying:
In order for UINavigationControllers
to send
"viewWill/Did/Appear/Disappear"
messages, it needs to have received
"viewWill/Did/Appear/Disappear" from
its container.
What is the container for my UINavigationControllers in this situation? myAppDelegate is defined in the .h file as:
NSObject <UIApplicationDelegate, CLLocationManagerDelegate>
and does not have a:
- (void)viewWillAppear:(BOOL)animated {
}
section. When I add one it says "NSObject may not respond to -viewWillAppear" in the debugger.
Any help out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 这相当简单。您需要设置 UITabBarController 的delegate 属性。您可以将其设置为拥有 tabbarcontroller 的控制器对象(或者您的应用程序委托,如果这是您的设置)。无论将哪个对象设置为委托,都将收到:
AND
来管理选项卡选择。将这些方法之一放置在您设置为委托的任何对象的实现中(您希望它是拥有所有控制器的对象)。
2) 如果您有一个显示为选项卡选择的 UIViewController(例如 ResortsListViewController),那么您需要自己将 viewWillAppear 方法放入控制器实现中:
让我知道如果我误解了你问题的第二部分。
1) This is fairly simple. You need to set the delegate property for the UITabBarController. You would set this to the controller object that owns your tabbarcontroller (or your app delegate if that is your setup). Whichever object is set as the delegate would then receive:
AND
to manage tab selections. Place one of these methods in the implementation of whatever object you set as the delegate (you would want it to be the object that owns all of the controllers).
2) If you have a UIViewController that appears as a tab selection (such as ResortsListViewController) then you would need to put the viewWillAppear method in the controller implementation yourself:
Let me know if I misunderstood the second part of your question.
好的,这里是:这是问题的正确答案,但是,它最终并没有那么难。我所要做的就是:
在 ResortViewController 中创建一个 int 类型的属性,变量名为whichChoice(例如)。然后在 TabBarController 的设置中解决它:
为了找出当我的 ResortsListViewController 接管时单击了哪个选项卡,我只需查询类变量:whichChoice 即可获得答案。
像这样的事情太简单了,你可以跳过它们。我认为您必须在 action 中传递变量并指定它与 target 的位置,就像在其他对象中所做的那样 - 但是当您在之前设置好东西时到时候你就不必这样做了。现在,话虽如此,动态设置“whichChoice”并不那么容易,无需更多思考......但将其设置为设置时已知的变量就可以了。
对于答案#2,我只是在我的类中放置了 IBOutlet 的变量样式,将其连接到表,然后按照您的指示进行操作,因为如果没有 IBOutlet 中的变量并将其连接到表,就没有对表视图。由于某种原因,将表简单地连接到 VIEW“Referencing Outlet”并调用
[self.tableview reloadData]
并不能完成这项工作。但在大多数情况下,你的答案是正确的,并引导我走向正确的方向。顺便说一句,如果您构建了 IB 元素,我真的很讨厌您必须在 IB 中右键单击并从这里拖动到那里。您应该能够使用它的 ObjectId (或其他东西)将其连接到代码中。这更符合程序员的要求。我知道 IB 的目的是让编程对于设计师来说更加容易,但是天哪......我很难理解!我最终大部分时间都放弃了 IB 并在代码中创建元素......我不知道执行起来是否一样快。我倾向于认为不是……但没有相反的证据。
Okay, here goes: This is the correct answer to the question, however, it did not end up being this hard. All I had to do was the following:
Create a property in ResortViewController of type int with variable name whichChoice (for instance). Then address it in the setup of the TabBarController a la:
To find out which tab was clicked when my resortsListViewController takes over, I simply query the class variable: whichChoice to get the answer.
Things like this are so simple you skip over them. I thought you had to pass the variable in an action and specify where it would go with the target like you do in other objects -- but when you set things up ahead of time you do not have to do that. Now, having said that, dynamically setting "whichChoice" is not so easy without a lot more thought...but setting it to a variable KNOWN at setup is fine.
For answer #2, I simply put a variable style of IBOutlet in my class, hooked it up to the table, and then followed your instructions, because without the variable in the IBOutlet and hooking it up to the table, there is no reference to the table view. For some reason hooking up the table simply to the VIEW "Referencing Outlet" and calling
[self.tableview reloadData]
did not do the job.But for the most part, your answers were right and led me in the right direction. As an aside, I really hate that you have to right-click and drag from here-to-there in IB if you have an IB element you built. You should be able to hook it up in code using it's ObjectId (or something). That would be more in line with programmers. I know IB is made to allow programming to be easier for designers, but gee wilikers..it is hard to wrap my mind around! I end up dropping IB and creating elements in code most of time time...which I do not know if is as fast to execute. I tend to think not...but have no proof to the contrary.