导航栏上的RightBar按钮没有像点击一样点击

时间:2016-06-06 08:34:38

标签: ios objective-c uibutton rightbarbuttonitem

我想在public class EntityVoorbeeld : DbContext { public DbSet<Student> Students { get; set; } /* ... the rest of your context's properties and methods ... */ } 上显示图片和标题,所以我编写了如下代码:

RightBar Button

此代码显示 UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease]; negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD; UIImage *buttonImage = [UIImage imageNamed:@"select-ipad@3x.png"]; UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom]; [selectButton setImage:buttonImage forState:UIControlStateNormal]; [selectButton setTitle:@"Select" forState:UIControlStateNormal]; [selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal]; [selectButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]]; [selectButton sizeToFit]; UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton]; [selectButton addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]]; 上的标题和图片,当我点击它时,点击的内容只会出现在图片上,而不会出现在文字上。

2 个答案:

答案 0 :(得分:0)

-(void)viewWillAppear:(BOOL)animated{    
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:@"select-ipad@3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;      
}

-(IBAction)onBtnRightClick:(id)sender
{
    NSLog(@"Right Bar Button Clicked.");
}

答案 1 :(得分:0)

使用您的代码,尝试2个按钮:

UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"select-ipad@3x.png"] forState:UIControlStateNormal];

UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
[button2 sizeToFit];

UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];

self.navigationItem.rightBarButtonItems = @[buttonItem2,buttonItem1];

我希望这可以帮到你。