2014-05-18 9 views
7

ho bisogno di un prompt di ingresso nella mia app e ho provato questoUIAlertView pronta ingresso

// Create a new item 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

E poi gestire in questo modo:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
// The user created a new item, add it 
if (buttonIndex == 1) { 
    // Get the input text 
    NSString *newItem = [[alertView textFieldAtIndex:0] text]; 
} 
} 

ma non sembra il clickedButtonAtIndex viene chiamato, perché?

Cordiali saluti, Erik

+0

perché il tag C#? – vikingosegundo

+0

oh, haha ​​- mi dispiace. Sono così abituato a chiedere quando si sviluppa per Windows Phone quindi è solo una vecchia abitudine – Erik

+0

Ohh. È davvero necessario downvotare tutto il tempo? Ora non posso fare domande ... ti sto chiedendo perché non ho trovato la soluzione da solo – Erik

risposta

7

è necessario impostare il delegato.

alert.delegate = self; //Or some other object other than self 

O quando si inizializzare l'avviso:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" 
               message:@"Enter a name for the item" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Add", nil]; 
0

metodo non viene chiamato perché non si è impostato delegato.
passare il self come delegato in modo da ottenere il riferimento ad esso.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 
+0

Dovevo scegliere una risposta, quindi ho scelto quello con meno reputazione :) - Ma grazie! – Erik