2013-01-07 12 views
9

Sto cercando di scoprire qual è il titolo UIButton per il quale UIButton è stato premuto nel seguente codice.Come ottenere il titolo per un UIButton quando viene premuto

sul viewDidLoad titolo tasto viene emesso alla console utilizzando:

 NSLog(@"The button title is %@ ", btn.titleLabel.text); 

vorrei ottenere questo titolo quando si preme un pulsante, invece.

Grazie per qualsiasi aiuto

:)

// Create buttons for the sliding category menu. 
     NSMutableArray* buttonArray = [NSMutableArray array]; 
     NSArray * myImages = [NSArray arrayWithObjects:@"category-cafe-unsel.png", @"category-food-unsel.png", @"category-clothing-unsel.png", @"category-health-unsel.png", @"category-tech-unsel_phone.png" , @"category-tech2-unsel.png", @"catefory-theatre-unsel.png", @"category-travel-unsel.png", nil]; 

     // only create the amount of buttons based on the image array count 
     for(int i = 0;i < [myImages count]; i++) 
     { 
      // Custom UIButton 

      btn = [UIButton buttonWithType:UIButtonTypeCustom]; 

      [btn setFrame:CGRectMake(0.0f, 20.0f, 52.0f, 52.0f)]; 
      [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal]; 
      [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal]; 

      NSLog(@"The button title is %@ ", btn.titleLabel.text); 

      [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
      [buttonArray addObject:btn]; 

      NSLog(@"Button tag is: %d",btn.tag); 

     } 

risposta

22

Nella tua azione:

- (void) buttonPressed:(UIButton*) sender { 
    NSLog(@"The button title is %@",sender.titleLabel.text); 
} 

Edit: O come commentato Iulian: sender.currentTitle, ma può essere nil, vedere i commenti di Michael.

+0

Qual è la differenza tra questo e 'sender.currentTitle'? –

+0

Nessuno. Hai ragione, modificherò la risposta. – Zaphod

+0

Non ho detto che non lo è, stavo solo chiedendo. E proprio ora 'currentTitle' era' nil' ma 'titleLabel.text' non lo era. –

11

Si dovrebbe avere la funzione da qualche parte ...

- (void)buttonPressed:(id)sender 

Basta mettere questo al suo interno ...

- (void)buttonPressed:(id)sender 
{ 
    UIButton *someButton = (UIButton*)sender; 

    NSLog(@"The button title is %@ ", [someButton titleForState:UIControlStateNormal]); 

    //You should also be able to use... 
    //NSLog(@"The button title is %@ ", someButton.titleLabel.text); 
} 
3
NSString * strButtonTitle = [btnButton titleForState:state]; 

dove Stato:

UIControlStateNormal, 
UIControlStateHighlighted, 
UIControlStateDisabled, 
UIControlStateSelected, 
UIControlStateFocused, 
UIControlStateApplication, 
UIControlStateReserved 

Per il vostro requisito

NSString * strButtonTitle = [btnButton titleForState:UIControlStateSelected]; 

Questo è il metodo per ottenere il titolo per i diversi stati del pulsante in un dato momento nel programma ... Ma in base alla domanda risposta da Zaphod è buono.

Problemi correlati