2012-12-02 7 views

risposta

6

Poiché li hai aggiunti a una barra di navigazione, è leggermente diverso, ma fondamentalmente lo stesso. Aggiungerai listener/handler nello stesso momento in cui crei il/i pulsante/i. Qui ho aggiunto << e >> ad una barra di navigazione utilizzando il seguente:

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]; 

e quindi creare i gestori come normale:

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

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

Bella spiegazione, grazie! – Tahlil

16

Un UIButton è una sottoclasse di UIControl.

Tutto ciò che devi fare dopo aver creato un pulsante è impostare il target e l'azione del pulsante. Ad esempio

// 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

E 'per iOS..So Si tratta di una sottoclasse di UIControl? ? destra? –