我试图将按钮添加到导航栏我想在没有任何操作的情况下添加按钮吗?
答案 0 :(得分:3)
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc]
initWithTitle:@"Your Button"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(methodName:)];
self.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
然后方法
-(IBAction)methodName {
// your code here
}
答案 1 :(得分:0)
您应该将UIBarButtonItem添加到导航栏而不是UIButton。你可以这样称呼:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.rightBarButtonItem = button;
答案 2 :(得分:0)
UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)];
[self.navigationItem setRightBarButtonItem:customBtn];
/////被叫事件
-(IBAction)customBtnPressed:(id)sender
{
//Your code here
}
答案 3 :(得分:0)
如果你真的想要UIButton,试试这个。
UIButton *btnSample = [[UIButton alloc] initWithFrame:btnFrame];
btnSample.backgroundColor = [UIColor blueColor];
UIBarButtonItem *barBtn_cart = [[UIBarButtonItem alloc] initWithCustomView:btnSample];
self.navigationItem.rightBarButtonItem = btnSample;