2013-02-24 13 views
7

Sto provando a creare un pulsante, che una volta toccato mostrerà un popover di un altro UIView. Per testare il tutto, ho il seguente codice nella mia sezione viewDidLoad:Codice codice UIButton azione

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height based on your specs 
    UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"]; 
    hard1.layer.cornerRadius = 10; 
    hard1.clipsToBounds = YES; 
    [hard1 addTarget: self 
       action: @selector(buttonClicked:) 
    forControlEvents: UIControlEventTouchUpInside]; 
    [self.hard1 setImage:buttonImage forState:UIControlStateNormal]; 
    [self.view addSubview:self.hard1]; 
} 

e più in basso:

- (IBAction) buttonClicked: (id)sender 
{ 
    NSLog(@"Tap"); 
} 

tuttavia, la console non registra 'Tap' quando premo il tasto. Qualche idea?

risposta

12

Guardate queste tre righe di codice:

hard1.layer.cornerRadius = 10; 
hard1.clipsToBounds = YES; 
[hard1 addTarget: self 
      action: @selector(buttonClicked:) 
forControlEvents: UIControlEventTouchUpInside]; 

Ti manca sé. in tutti e tre. Dovrebbero essere:

self.hard1.layer.cornerRadius = 10; 
self.hard1.clipsToBounds = YES; 
[self.hard1 addTarget: self 
      action: @selector(buttonClicked:) 
forControlEvents: UIControlEventTouchUpInside]; 

Inoltre, se si sta creando programatically, non dovrebbe essere un IBAction (IB sta per la creazione di interfacce e questo non viene creato in Interface Builder).

+0

Ah, stupido. Grazie mille! –

+1

Ora che ci sono. Quando si crea una proprietà, eseguire questa operazione in sintetizzare: @synthesize hard1 = _hard1; In questo modo il riferimento alla proprietà come solo "hard1" non funzionerà. È self.hard1 o _hard1 (quest'ultimo solo da utilizzare nel metodo setter) – Odrakir

+0

grazie mille per il vostro aiuto –

0

L'utente ha risposto all'evento come buttonClicked ma in IBAction è stato definito buttonTap. Questo non sarebbe ovviamente funziona ...

Va

[hard1 addTarget: self 
       action: @selector(buttonTap:) 
    forControlEvents: UIControlEventTouchUpInside]; 
+0

ho fatto un errore di battitura. Il mio codice attuale è stato modificato sopra, continua a non funzionare. –

3
self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom]; 


[hard1 addTarget: self 
       action: @selector(buttonClicked:) forControlEvents: UIControlEventTouchUpInside]; 

Sostituire grattugiare 1 a self.hard1

Problemi correlati