2012-08-13 14 views
13

Chiunque può aiutarmi su come aggiungere un'azione per chiamare il metodo seguente utilizzando UIButton.Aggiungi target per UIButton - iOS

-(void)navigatePics:(id)sender andIndex:(NSInteger *)index{} 
+0

Si prega di spiegare che tipo di funzionalità che si desidera. Vuoi che qualcuno scriva tra {} per fare in modo che il metodo faccia qualcosa? O hai bisogno di aiuto per collegarlo all'interfaccia Builder (IB)? – MrHappyAsthma

+0

Sto solo cercando il [pulsante addTarget: self action: @selector (_questo spazio____) forControlEvents: UIControlEventTouchUpInside]; –

risposta

50

Utilizzare il button.setTag: il metodo (NSInteger) per aggiungere l'indice del UIButton come un tag.

UIButton *button = ...; 
[button setTag:1234]; 
[button addTarget:self action:@selector(navigatePics:) forControlEvents:UIControlEventTouchUpInside]; 

Quindi in navigatePics, leggere il tag.

-(void)navigatePics:(UIButton *)sender 
{ 
int index = sender.tag; 
// index = 1234; 
} 
0

Questo dovrebbe fare il trucco. Non usare self se vuoi chiamare questo metodo su un'altra istanza. In tal caso, basta usare quell'istanza al posto di sé.

[button addTarget:self action:@selector(navigatePics:andIndex:) forControlEvents:UIControlEventTouchUpInside]; 
+0

come potrei passare il valore dell'indice? –

+0

non è possibile passare il valore dell'indice ... ottenerlo dal mittente –

2
UIButton *button = [[UIButton alloc] init]; 
button.tag = 4; //index 
[button addTarget:self @selected(buttonDidTap:)]; 

... 
[button release]; 

-(void)buttonDidTap:(UIButton *)sender{ 
NSInteger index = sender.tag; 
}