没有UITabBarController的UITabBar中的初始选定项

时间:2009-09-03 20:19:20

标签: iphone objective-c

如何在UITabBar中设置 默认 选中的UITabBarItem,而不是UITabBarController?

为了澄清,UIView确实实现了协议,并且didSelectItem方法也起作用。在运行时,tabbar工作,并在用户触摸它们时选择tabbaritems。我的问题是设置默认的选定项目。

如果我在didSelectItem方法中使用[myTabbar setSelectedItem]它可以工作。但除此之外,它没有(例如,在我的UIView的viewDidLoad方法中)。

谢谢!

2 个答案:

答案 0 :(得分:4)

感谢您的批准,但我仍然遇到一个问题,即当我选择带有setSelectedItem的项目时,不会调用didSelectedItem。有什么想法吗?

实际上,我确实使用了它有点不同:

[tabbar setSelectedItem:[tabbar.items objectAtIndex:0]];

最后我这样解决了......

- (void)viewDidLoad 
{
 UITabBarItem *item = [myTabBar.items objectAtIndex:0];
 [self.myTabBar setSelectedItem:item];
 if(tab1Exam == nil){
  self.tab1Exam = [[CurExam alloc] initWithNibName:@"CurExam" bundle:nil];
 [self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
 if (currentViewController != nil)
  [currentViewController.view removeFromSuperview];
 currentViewController = tab1Exam;
 }
}

答案 1 :(得分:2)

您可能想发布一些示例代码。我刚刚在-viewDidLoad中进行了快速测试:

UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:@"About" image:[UIImage imageNamed:@"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];

工作正常,或者至少按照我的预期工作。