2010-03-25 55 views
10

我创建中的UITabBar一样,并没有UITabBarItems上的UITabBarController它,现在我想知道如何把一个动作的UITabBarItem.What的点击都是我应该在UITabBarItem行动使用的方法?如何为UITabBarItem创建动作?

+0

根据人机接口指南,选项卡栏用于切换视图。你确定你不想使用工具栏吗? – 2010-03-26 00:24:52

回答

2

您是否使用一个UINavigationController?如果是这样,从活动视图控制器子类,你得到的navigationItem和按钮添加到它,例如:

- (void) viewWillAppear:(BOOL)animated; 
{ 
    [super viewWillAppear: animated]; 
    UIBarButtonItem * leftButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"Don't Show Again" style: UIBarButtonItemStyleBordered target: self action: @selector(permanentlyCloseWelcomeView)] autorelease]; 
    [[self navigationItem] setLeftBarButtonItem: leftButtonItem]; 
} 
+0

不...我没有使用任何控制器.. – suse 2010-03-25 07:02:33

20

不能直接设置UITabBarItem对象的动作。相反,您的视图控制器应该实现以下UITabBarDelegate方法:

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

当用户选择一个选项卡这种方法被称为(即UITabBarItem)。

+0

这工作对我来说,但记住要将tabbar的委托设置为'self'in viewdidload() – crackles 2017-06-29 07:56:11

-2

没有比didSelectItem一个更好的方法:对于每个TabBarItem 您创建一个动作:
[item1 setAction:@selector(pressItem1:)];
[item2 setAction:@selector(pressItem2:)];
[item3 setAction:@selector(pressItem3:)];
[item4 setAction:@selector(pressItem4:)];
然后你就可以使用新的动作:

-(void)pressItem1:(UITabBarItem *) item1 {<br/> 
    // Here comes your code which<br/> 
    // occurs after pressing item1.<br/> 
} 

这对我来说

+1

您正在使用私有API。苹果不会批准它 – user102008 2011-04-23 07:10:41

+0

似乎是的。 Ita私人API。 – Dilshan 2012-08-26 10:34:08

1

能运作的,你逃避使用UIToolbar和UIBarButtonItem的实例?这可能更直接。

toolBar = [[UIToolbar alloc] init]; 
newPlayerItem = [[UIBarButtonItem alloc] initWithTitle:@"+" 
            style:UIBarButtonItemStyleBordered 
            target:self 
            action:@selector(newPlayer:)]; 

NSArray *toolBarItemsArray = [[NSArray alloc] initWithObjects:newPlayerItem, nil]; 
[toolBar setItems:toolBarItemsArray]; 
[toolBarItemsArray release];