學習筆記:Tab Bar 控件使用詳解


注意這里是:Tab Bar 不是Tab Bar Controller. Tab bar是繼承UIView,所以可以添加到ViewController里。是View就可以add到另一個View上去。Tab Bar Controller是新建View視圖。

XX.h 里需要定義UITabBar,並且要引用協議 UITabBarDelegate.

@interface LoginViewController :UIViewController<UITabBarDelegate>

{

    UITabBar *tabbar;

}

@property (nonatomic, retain) UITabBar *tabbar;

 

XX.m

@synthesize tabbar;

- (void)viewDidLoad

{

    //加載Tab bar

    CGRect footFrame = CGRectMake(0, 420, 320, 60);

    tabbar = [[UITabBar alloc]initWithFrame:footFrame];

    

    UITabBarItem *item1 = [[UITabBarItem alloc]initWithTabBarSystemItem:1 tag:0];

    UITabBarItem *item2 = [[UITabBarItem alloc]initWithTabBarSystemItem:2 tag:1];

    NSArray *items = [[NSArray alloc]initWithObjects:item1,item2, nil];

    [tabbar setItems:items animated:YES];

    [item1 release];

    [item2 release];

    [items release];

    [self.view addSubview:tabbar];

    tabbar.delegate = self;//指定其代理方法,不然方法不起作用

    [tabbar release]; 

}

- (void)tabBar:(UITabBar *)tabbar didSelectItem:(UITabBarItem *)item 

{

    NSLog(@"Selected is %d",item.tag);

    NSString *msg = [[NSString alloc]initWithFormat:@"selected is %d",item.tag];

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"a" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

    [msg release];

    [alert release];

}

 

gwesley 2011-08-14 08:57
給每個item添加一個tag
然后在此方法中 如下判斷
switch(item.tag) {
case 0:

break
case 1:

break


patrick_ren 2011-08-14 10:44
- (void)tabBar:(UITabBar *)tb didSelectItem:(UITabBarItem *)item
這個方法有要求一定要寫在那兒嗎?

patrick_ren 2011-08-14 17:10
好像沒發調用啊,是不是還要在viewdidload里寫東西啊?

exphinx 2011-08-14 18:26
- tabBar:didSelectItem: 是UITabBar的delegate方法,你給它設定delegate了嗎?

patrick_ren 2011-08-14 19:37
設定了,在頭文件里,對嗎?

patrick_ren 2011-08-14 20:22
解決了,忘記寫tb.delegate = self了~謝謝各位~

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM