2012-12-02 119 views

回答

6

既然你已經將它們添加到導航欄中,它有點不同,但基本相同。您將在創建按鈕的同時添加偵聽器/處理程序。在這裏,我已經添加了<<>>使用導航欄下面:

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@">>" style:UIBarButtonItemStylePlain target:self action:@selector(navNextButtonPressed)]; 
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(navPrevButtonPressed)]; 
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:nextButton, prevButton, nil]; 

,然後創建您的處理程序正常:

#pragma mark - button handling 
-(void)navNextButtonPressed 
{  
    NSLog(@"Next pressed"); 
} 

-(void)navPrevButtonPressed 
{ 
    NSLog(@"Prev pressed"); 
} 
+0

很好的解釋謝謝! – Tahlil

16

UIButton是UIControl的一個子類。

創建按鈕後,您需要執行的所有操作都是設置按鈕的目標和操作。即

// Create your button: 
UIButton *button = // However you create your button 

// Set the target, action and event for the button 
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain. 
      action:// A selector for the method 
forControlEvents:UIControlEventTouchUpInside]; 
+2

這對iOS..So它是UIControl的子類? ?對? –

相關問題