【问题标题】:Tab bar Items not showing properly标签栏项目未正确显示
【发布时间】:2011-09-16 15:25:20
【问题描述】:

图 1 显示了在应用程序启动时加载的动态创建的标签栏控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    autoMagically = [[AutoMagically alloc] initWithNibName:nil bundle:nil];

    [self.window addSubview:autoMagically.view];
    [self.window makeKeyAndVisible];

    return YES;
}

如果我通过单击视图上的按钮来加载它(我想要并且需要这样做的方式),我会得到上图图 2 所示的内容:

-(void) loadWhenClicked{
AutoMagically  *todaysDeal = [[AutoMagically alloc] initWithNibName:nil bundle:nil];
    todaysDeal.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:todaysDeal animated:YES];
    [todaysDeal release];

}

这是我创建标签栏的代码:

tabBarController = [[UITabBarController alloc] init];

FirstViewController* vc1 = [[FirstViewController alloc] init];

SecondViewController* vc2 = [[SecondViewController alloc] init];

vc1.title = @"Dallas";//[[NSUserDefaults standardUserDefaults] objectForKey:@"Citynamefrmhome"];
vc1.tabBarItem.image = [UIImage imageNamed:@"Dealss.png"];

vc2.title = @"My Vouchers";
vc2.tabBarItem.image = [UIImage imageNamed:@"nav_voucher_S.png"]; 

NSArray* controllers = [NSArray arrayWithObjects:vc1,vc2, nil];

tabBarController.viewControllers = controllers;

[self.view addSubview:tabBarController.view];

当我像其他视图一样加载标签栏控制器时,如何使标签栏控制器正确显示,如图 1 所示? 这是一个使用 xcode 4 的基于 iphone 视图的应用程序。

【问题讨论】:

标签: objective-c ios4 xcode4


【解决方案1】:

所以问题是 UITabController 是一个视图控制器,所以它并不是你想用 addSubview 添加到显示中的东西(因为它不是一个视图)。相反,修改你的代码来做到这一点,你会没事的:

UITabBarController *todaysDeal = [[UITabBarController alloc] init];

FirstViewController* vc1 = [[FirstViewController alloc] init];

SecondViewController* vc2 = [[SecondViewController alloc] init];

vc1.title = @"Dallas";//[[NSUserDefaults standardUserDefaults] objectForKey:@"Citynamefrmhome"];
vc1.tabBarItem.image = [UIImage imageNamed:@"Dealss.png"];

vc2.title = @"My Vouchers";
vc2.tabBarItem.image = [UIImage imageNamed:@"nav_voucher_S.png"]; 

NSArray* controllers = [NSArray arrayWithObjects:vc1,vc2, nil];

todaysDeal.viewControllers = controllers;

todaysDeal.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:todaysDeal animated:YES];
[todaysDeal release];

希望这会有所帮助。

【讨论】:

  • 您好,感谢您的回复。我正在 viewDidLoad 中创建标签栏控制器。我把这段代码放在那里吗?另外,我看不到标签栏控制器被添加到视图的位置。您创建它,然后它转换,就好像它调用另一个视图来加载一样。请原谅我缺乏这方面的知识。
  • 问题是,如果你在 viewDidLoad 中添加它,那么每当你创建 UIViewController 时,你就是在创建 UIViewController。这只是没有必要。所以基本上只需使用 UITabBarController 作为你唯一的 UIViewController ...根本不需要 AutoMagically。有意义吗?
  • 成功了!!我没有意识到标签栏控制器本身就是一个视图控制器。我已经问过这个问题,直到现在才得到答案。谢谢!!我会请你吃午饭,甚至通过 PayPal 给你寄一些东西。我已经为此工作了 2 天。谢谢!!
  • 酷,我会为你解决的,但你投票赞成我的答案;)
猜你喜欢
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 2012-07-22
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多