2011-08-21 28 views
0

我放入了一個自定義的「後退」UIBarButtonItem,它來自我在Photoshop中製作的圖像。我設法使它看起來很好,我的問題是讓它工作。自定義UIButton從表視圖向上鑽取?

我的按鈕什麼都不做,我假設,因爲我還沒有分配任何行動。我只是不知道這樣做的正確語法?

繼承人我的代碼

UIImage *buttonImage = [UIImage imageNamed:@"BackButton.png"]; 
     UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [backButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
     backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); 

// Im assuming this is where i need to put the action... 
     //[backButton addTarget:self action:@selector(.......) forControlEvents:UIControlEventTouchUpInside]; 

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
               initWithCustomView:backButton]; 

基本上,我想我的新的自定義後退按鈕的功能就像一個默認的規則的UITableView

問候, Andyy

回答

2

你可以做到這一點IB,或者您可以在addTarget:action:forControlEvents:UIControlEventTouchUpInside調用的代碼中進行調用,以便在點擊按鈕時調用方法(操作)。例如:

[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

這在UIControl class reference記載,是的UIButton UIControl的子類。

您需要一種方法,當事件發生時我會調用backButtonPressed:。此方法採用一個參數,發送事件的對象,如下所示:

-(void)backButtonPressed:(id)sender 
{ 
    // NOTE: assumes self is your UIViewController 
    [self.navigationController popViewControllerAnimated:YES]; 
}