使 TabBarItem 在启动时被选中
我使用 Xcode 4 处理 iPhone iOS4 的项目。
我在 IB 中创建了一个 UITabBar,其中包含三个 UITabBarItem。我没有 UITabBarControllerDelegate 作为委托,但有 MainViewController。
然后我在 MainViewController 中实现:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
[scrollView setContentOffset:(CGPointMake (0,0))];
break;
case 2:
[scrollView setContentOffset:(CGPointMake (320,0))];
break;
case 3:
[scrollView setContentOffset:(CGPointMake (640,0))];
break;
default:
break;
}
NSLog(@"didSelectItem: %d", item.tag);
}
这工作正常,正如我想要的那样。
但我无法在启动时选择 TabBarItem(在 viewDidLoad 中)。我尝试了
[tabBar setSelectedItem:calc];
[tabBar setSelectedItem:1];
(cal是MainViewController的ivar)和其他人,但没有一个有效。有什么建议吗?
谢谢。
这是MainViewController.h
#import "FlipsideViewController.h"
#import <CoreData/CoreData.h>
#import <QuartzCore/QuartzCore.h>
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UITabBar *tabBar;
UITabBarItem *diagBarItem;
UITabBarItem *calcBarItem;
}
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;
@property (nonatomic, retain) IBOutlet UITabBarItem *diagBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem *calcBarItem;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
@end
I work on a project for iPhone iOS4 with Xcode 4.
I created a UITabBar, with three UITabBarItem, in IB. I don't have UITabBarControllerDelegate as delegate, but MainViewController.
Then I implement in MainViewController:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
[scrollView setContentOffset:(CGPointMake (0,0))];
break;
case 2:
[scrollView setContentOffset:(CGPointMake (320,0))];
break;
case 3:
[scrollView setContentOffset:(CGPointMake (640,0))];
break;
default:
break;
}
NSLog(@"didSelectItem: %d", item.tag);
}
This works OK, as I want it.
But I'm not able to make a TabBarItem selected at startup (in viewDidLoad). I try
[tabBar setSelectedItem:calc];
[tabBar setSelectedItem:1];
(cal is an ivar of MainViewController) and others but none works. Any suggestion?
Thank you.
This is MainViewController.h
#import "FlipsideViewController.h"
#import <CoreData/CoreData.h>
#import <QuartzCore/QuartzCore.h>
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UITabBar *tabBar;
UITabBarItem *diagBarItem;
UITabBarItem *calcBarItem;
}
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;
@property (nonatomic, retain) IBOutlet UITabBarItem *diagBarItem;
@property (nonatomic, retain) IBOutlet UITabBarItem *calcBarItem;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在标签栏上调用
-setSelectedItem:
用于以编程方式将 UITabBarItems 移动到标签栏上的不同位置。要更改选定的选项卡,您需要更改 UITabBarController 实例上的
selectedIndex
属性。UIViewController
上的属性tabBarController
由 Interface Builder 自动设置。在-viewDidLoad
中尝试一下:Calling
-setSelectedItem:
on your tab bar is used to move UITabBarItems to different spots on the tab bar programmatically.To change the selected tab, you need to change the
selectedIndex
property on the instance of UITabBarController. The propertytabBarController
onUIViewController
is automatically set by Interface Builder. Try this in-viewDidLoad
:您需要在
UITabBarController
上设置选定的选项卡,而不是在选项卡栏本身上:正如您所说,这不起作用,您确定已在 Interface Builder 中连接了插座吗? (假设您使用它来创建选项卡栏)
You need to set the selected tab on the
UITabBarController
, and not on the tab bar itself:As you said this does not work, are you sure you have connected the outlets in Interface Builder? (Assuming that you used that to create the tab bar)
试一试吧!
Give this a shot!