Add action to UIButton which in a customview
I have a custom view and it has a UIButton. I add this view to my navigationItem's rightbarButtonItem, it's ok, but I have to add an action to this button.
Here is my code;
-(void)showMessage:(id)sender
{
UIAlertView *alert = [[UIAlertV开发者_StackOverflow社区iew alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
[buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
[buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
buttonView.view.frame = CGRectMake(0,0,95,34);
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view];
self.navigationItem.rightBarButtonItem = item;
[self setTitle:@"Details"];
[super viewDidLoad];
}
Thanks.
You don't need a custom view with a UIButton, what you need is alloc and init a UIBarButtonItem and put it on your nav bar
the code would look like:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back Button") style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
精彩评论