学习笔记: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];

}

 

gwesley2011-08-14 08:57
给每个item添加一个tag
然后在此方法中 如下判断
switch(item.tag) {
case 0:

break
case 1:

break


patrick_ren2011-08-14 10:44
- (void)tabBar:(UITabBar *)tb didSelectItem:(UITabBarItem *)item
这个方法有要求一定要写在那儿吗?

patrick_ren2011-08-14 17:10
好像没发调用啊,是不是还要在viewdidload里写东西啊?

exphinx2011-08-14 18:26
- tabBar:didSelectItem: 是UITabBar的delegate方法,你给它设定delegate了吗?

patrick_ren2011-08-14 19:37
设定了,在头文件里,对吗?

patrick_ren2011-08-14 20:22
解决了,忘记写tb.delegate = self了~谢谢各位~

 

 

转载于:https://www.cnblogs.com/martin1009/archive/2012/06/08/2542171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值