How to get tabbar click event without tabbarcontroller in iphone?
I am trying to implement tabbar in my 2nd view. I am开发者_开发技巧 able to place tabbar with 5 items on it. I know to handle those click events , i have to use tabbarcontroller.
My question is , taking tabbar on view,
How to call each item selected method without tabbarcontroller
?
(My assumption is that tabbar
is an object like button , and we can write a click method for that programmatically. So without Tab Controller also, we can access selected item method )
Is there any way to do that?
If you don’t want to use tabbarcontroller, then its always better to use Segmented Control. It has similar kind of operations & its also simple to use. Use need to just create multiple uiviews on view & just play Hide-n-seek with them. If you want I have some code. Will paste here, if you want.
I think, you can do it like this. (*** Note : Its not tested code)
Add <UITabBarDelegate>
in .h file
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item == firstItem)
NSLog(@"Did Select Here”);
else if(item == firstItem)
NSLog(@"Did Select Here”);
}
Create UITabbar
and define UITabbarDelegate
in .h file and write the following code in respective view:
- (void)viewDidLoad {
[super viewDidLoad];
UITabBarItem * newItem1 = [[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"setting.png"] tag:1];
UITabBarItem * newItem2 = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"setting.png"] tag:2];
UITabBarItem * newItem3 = [[UITabBarItem alloc] initWithTitle:@"Third" image:[UIImage imageNamed:@"setting.png"] tag:3];
[tabbar setItems:[NSArray arrayWithObjects:newItem1,newItem2,newItem3,nil]];
tabbar.delegate = self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(@"Tabbar selected itm %d",item.tag);
switch (item.tag) {
case 1:
//first selected
break;
case 2:
//second selected
break;
case 3:
//third selected
break;
default:
break;
}
}
精彩评论