我在新的iOS Development.I有UINavigationController
作爲RootViewController的爲UIWindow
。在我加入違約Tabbarcontroller選擇作爲第一控制器UITabbarController
programatically.I的子類。在這裏,我正在試圖將3個按鈕添加到導航欄。如何添加UIButtons到導航欄,班級也包括的UITabBarController
任何人都可以發送給我的代碼。 由於事先
我在新的iOS Development.I有UINavigationController
作爲RootViewController的爲UIWindow
。在我加入違約Tabbarcontroller選擇作爲第一控制器UITabbarController
programatically.I的子類。在這裏,我正在試圖將3個按鈕添加到導航欄。如何添加UIButtons到導航欄,班級也包括的UITabBarController
任何人都可以發送給我的代碼。 由於事先
下面是簡單的代碼,以便在UINavigationBar
添加UIButton
在下面的代碼,您可以添加按鈕使用FlexibleSpace
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStyleBordered target:self action:@selector(FirstTapped:)];
[barItems addObject: btnFirst];
UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStyleBordered target:self action:@selector(SecondTapped:)];
[barItems addObject:btnSecond];
UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:@"Third" style:UIBarButtonItemStyleBordered target:self action:@selector(ThirdTapped:)];
[barItems addObject: btnThird];
self.navigationItem.rightBarButtonItems = barItems;
按鈕相關的方法
-(void) FirstTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) SecondTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) ThirdTapped:(UIBarButtonItem *)sender{
//perform your action
}
注意:self.navigationItem.rightBarButtonItems
只適用於iOS 5或後者。
要添加在左邊或右邊,你必須調用下列方法之一多個按鈕:
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated
數組items
包含UIBarButtonItem
類的實例。您可以實例化他們這樣
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(buttonTapped:)];
然後,你必須實現選擇buttonTapped:
當按鈕被敲擊時調用。
-(void)buttonTapped:(UIBarButtonItem *)button
{
// do the things that should happen when the button is pressed
}
如果你不想讓他們在左側或右側,也可以創建一個UIView
自己包含的按鈕和設置視圖是UINavigationItem
的titleview的。這種效果是在Facebook的應用程序
[self.navigationItem setTitleView:yourView];
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
// here is custom button setup //
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
類似按鈕難道不應該是' - (無效)FirstButtonTapped {// 碼相關的功能 }' ? – 2013-02-22 08:30:09