右栏按钮项不起作用

时间:2011-10-31 10:25:45

标签: iphone ios5 navigationbar

实际上我在导航栏的右侧放置一个条形按钮,它不起作用。但当我用它作为左栏按钮项目时,它工作正常。我正在使用ios5。

当我同时按下左右按钮时,它也无法正常工作。然后我为两者设置框架,然后这些都有效。但是当我右侧只有一个按钮时,它就无法正常工作。

 UIButton *but1 = [UIButton buttonWithType:UIButtonTypeCustom];//customising map button.

but1.frame = CGRectMake(270,0,50,40);       

[but1 addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];//on cilcking an map button clicked method is called.

buttonRight = [[UIBarButtonItem alloc]initWithCustomView:but1];//setting map button on Navigation bar.

self.navigationItem.rightBarButtonItem = buttonRight;//setting button on the Right of navigation bar.

如何找出此错误?

3 个答案:

答案 0 :(得分:2)

UIBarButtonItem *addButton=[[UIBarButtonItem alloc]initWithTitle:@"edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit_details)];
self.navigationItem.rightBarButtonItem=addButton;

检查它是否有效

答案 1 :(得分:1)

    UIToolbar* toolbar = [[UIToolbar alloc]
                          initWithFrame:CGRectMake(0, 0, 50, 45)];
    [toolbar setBarStyle: UIBarStyleBlackOpaque];
    // create an array for the buttons
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:0];

    // create a standard BarButtonItem
    UIBarButtonItem *SettingsBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_setting.png"] style:UIBarButtonItemStylePlain
                                   target:self
                                   action:@selector(ActionMethod:)];
    [buttons addObject:SettingsBtn];
    [SettingsBtn release];

    // put the buttons in the toolbar and release them
    [toolbar setItems:buttons animated:NO];
    [buttons release];

    // place the toolbar into the navigation bar
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                              initWithCustomView:toolbar]autorelease];
    [toolbar release];

答案 2 :(得分:0)

对于 Swift 4.2 代码:

在View中加载功能

let addButton = UIBarButtonItem(title: "edit", style: .plain, target: self, action: #selector(edit))
 navigationItem.rightBarButtonItem = addButton

函数定义在这里:

@objc func edit() {
    // Body definition
}