可以直接看代码,可以直接复制,自己运行:
VlinkageViewController.h(实现UITabBarDelegate)
VlinkageViewController.h1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#import <UIKit/UIKit.h>
@interface VlinkageViewController : UIViewController<UITabBarDelegate>
@property (nonatomic, retain) UITabBar *tabBar;
@end
|
VlinkageViewController.m(关键一点是要实现[self.tabBar setDelegate:self],不然不起作用的)
VlinkageViewController.m1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#import <QuartzCore/QuartzCore.h> #import "VlinkageViewController.h"
@interface VlinkageViewController ()
@end
@implementation VlinkageViewController
@synthesize tabBar;
- (void)viewDidLoad { [super viewDidLoad]; CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGFloat width = screenBounds.size.width; CGFloat height = screenBounds.size.height; #pragma 添加tabBar CGFloat tabBarHeight = 40; CGFloat tabBarY = height - tabBarHeight - searchBar.frame.size.height / 2; self.tabBar = [[UITabBar alloc] initWithFrame: CGRectMake(0, tabBarY, width, tabBarHeight)]; [self.tabBar setDelegate:self]; UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"查找" image:nil tag:0]; UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"我的关注" image:nil tag:1]; UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTitle:@"咨询" image:nil tag:2]; UITabBarItem *tabBarItem4 = [[UITabBarItem alloc] initWithTitle:@"更多" image:nil tag:3];
NSLog(@"tabBarItem1.tag = %d",tabBarItem1.tag); NSLog(@"tabBarItem1.tag = %d",tabBarItem2.tag); NSLog(@"tabBarItem1.tag = %d",tabBarItem3.tag); NSLog(@"tabBarItem1.tag = %d",tabBarItem4.tag); NSArray *tabBarItemArray = [[NSArray alloc] initWithObjects:tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4, nil]; [self.tabBar setItems:tabBarItemArray]; [self.view addSubview:self.tabBar]; }
-(void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"item.tag= %d", item.tag); }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
|