2010-12-17 111 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

相關問題