0

我是 iOS Development 的新手。我有UINavigationController作为 rootviewcontroller 的。在我以编程方式UIWindow添加的子类中。我将UITabbarController默认的 Tabbarcontroller 选择作为第一个控制器。在这里,我尝试向导航栏添加 3 个按钮。

任何人都可以给我发送正确的代码。提前致谢

4

3 回答 3

0

要在左侧或右侧添加多个按钮,您必须调用以下方法之一:

- (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. 此效果类似于 Facebook App 中的按钮

[self.navigationItem setTitleView:yourView];
于 2013-02-22T08:35:04.273 回答
0
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];

// here is custom button setup //

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];

[self.navigationItem setLeftBarButtonItem:item animated:YES];
于 2013-02-22T10:07:51.393 回答
0

这是添加的简单UIButton代码UINavigationBar

在以下代码中,您可以添加 Button WithFlexibleSpace

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 或更高版本。

于 2013-02-22T08:27:53.087 回答