标签栏项目未正确显示

时间:2011-09-16 15:25:21

标签: objective-c ios4 xcode4

Picture

图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视图的应用程序。

1 个答案:

答案 0 :(得分:0)

所以问题是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];

希望这有帮助。

相关问题