如果您沒有使用標籤欄控制器進行任何複雜操作,您可以從UITabBar滾動自己,然後在切換選項卡時可以執行任何操作。您只需實施UITabBarDelegate
。下面是我使用的是在應用的哪些方面稍微修改示例代碼:
.h文件中:
@interface AllGames : UIViewController <UITabBarDelegate> {
IBOutlet UITabBar *tabBar;
}
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;
.m文件:
@implementation AllGames
@synthesize tabBar;
UIViewController *subviews[3];
- (void)viewDidLoad {
[super viewDidLoad];
int startTab = [Preferences LookupPreferenceInt:CFSTR("GameTab")];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:startTab]];
Games1 *vc1 = [[Games1 alloc] initWithNibName:@"Games1" bundle:nil];
Games2 *vc2 = [[Games2 alloc] initWithNibName:@"Games2" bundle:nil];
Games3 *vc3 = [[Games3 alloc] initWithNibName:@"Games3" bundle:nil];
subviews[0] = vc1;
subviews[1] = vc2;
subviews[2] = vc3;
vc1.view.alpha = 0;
vc2.view.alpha = 0;
vc3.view.alpha = 0;
[self.view addSubview:vc1.view];
[self.view addSubview:vc2.view];
[self.view addSubview:vc3.view];
subviews[startTab].view.alpha = 1.0;
}
- (void)dealloc {
[Preferences StorePreferenceInt:CFSTR("GameTab") withValue:[tabBar selectedItem].tag-1];
for (int x = 0; x < 3; x++)
{
[subviews[x].view removeFromSuperview];
[subviews[x] release];
subviews[x] = 0;
}
[super dealloc];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
printf("Hit item tag: %d\n", item.tag);
for (int x = 1; x <= 3; x++)
{
subviews[x-1].view.alpha = (x == item.tag)?1.0:0.0;
}
}
裏面didSelectItem你可以做任何你的動畫想。我只是立即交換alpha,但你可以像你想的那樣複雜。
你有什麼例子嗎?它會幫助我瞭解要做什麼。 – intl 2010-03-05 14:29:17
用一個詳細的例子編輯上面的代碼。 – 2010-03-05 16:32:07