2010-12-17 78 views
0

我有一个自定义视图,它有一个UIButton。我将这个视图添加到我的navigationItem的rightbarButtonItem,没关系,但我必须添加一个动作到这个按钮。添加动作到自定义视图中的UIButton

这是我的代码;

-(void)showMessage:(id)sender 
{ 
    UIAlertView *alert = [[UIAlertView 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]; 
} 

谢谢。

回答

1

你并不需要一个UIButton,你需要的是alloc和初始化一个的UIBarButtonItem自定义视图,并把它放在你的导航栏上

的代码是这样:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back Button") style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)]; 

self.navigationItem.leftBarButtonItem = backButton; 
[backButton release]; 
+0

是的,但我可能会添加更多的按钮或其他东西,所以我需要在自定义视图中进行操作。有什么想法吗? – Can 2010-12-17 10:15:41

相关问题