Obj-C,UITabbarController 中的 UINavigationController,简单解释一下?
我正在使用一些示例代码来尝试一劳永逸地弄清楚如何使导航控制器和选项卡控制器一起工作。作为奖励,没有内存泄漏。
遇到如下所示的问题...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc]
initWithNibName:@"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:self.tabBarController]; <<<<
[self.window makeKeyAndVisible];
return YES;
在我的主项目中,我有一个用于 4 个不同导航控制器的插座,我这样称呼每个控制器。
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication]
delegate];
[delegate.balNavController pushViewController:nextController animated:YES];
但这会泄漏并引发问题。
如果不使用界面生成器,有人可以用简单的术语建议我应该如何做到这一点,也许只需要几行代码。
I'm playing with some sample code to try and figure out once and for all how to make navigation controller (s) and a tab controller to work together. As a bonus without memory leaks.
Having problems as shown below...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc]
initWithNibName:@"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:self.tabBarController]; <<<<
[self.window makeKeyAndVisible];
return YES;
In my main project I have an outlet for 4 different navigation controllers and I call each like so.
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication]
delegate];
[delegate.balNavController pushViewController:nextController animated:YES];
But this leaks and is causing problems.
Without using the interface builder, can someone advise me, in simple terms how I should be doing this, perhaps with a few lines of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈建议您获取一份Big Nerd Ranch iPhone 编程指南。它有出色的阐述和示例,可帮助您精通基础知识。现在讨论您的问题...
通过给它一个
UIViewController
甚至UINavigationController
的NSArray
来设置UITabBarController
>s。标签栏不介意哪个。UINavigationController
是通过给它一个作为根的UIViewController
来设置的。请记住,选项卡栏不是视图控制器,因此它不能是导航控制器的根!
将这些组合起来只是一个正确顺序的问题。这是一个希望能说明这一点的通用示例。
I strongly encourage your to pick up a copy of the Big Nerd Ranch Guide to iPhone Programming. It has excellent exposition and examples to get you proficient in the basics. Now on to your issues...
A
UITabBarController
is set by giving it anNSArray
ofUIViewController
s or evenUINavigationController
s. The Tab Bar doesn't mind which.A
UINavigationController
is set by giving it aUIViewController
that will be the root.Keep in mind that the Tab Bar is not a View Controller, so it cannot be the root of a Navigation Controller!
Combining these is simply a matter of correct order. Here's a generic example that hopefully illustrates this.