2013-03-12 6 views
0

Я хочу проверить, если пользователь нажимает «да», например, я хочу поставить определенное действие.Как проверить возвращаемое значение UIAlertView?

Это моя строка кода:

UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"this will start a new game" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 

    [mes show]; 

Так что я хочу сказать if user tap "yes" preform this action

это мой метод: Я хочу сказать, если пользователь нажмет «да» создать новую игру.

- (IBAction)newGame:(UIButton *)sender { 

    UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"this will start a new game" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 

    [mes show]; 


    self.flipsCount = 0; 
    self.game = nil; 
    for (UIButton *button in self.cardButtons) { 
     Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]]; 
     card.unplayble = NO; 
     card.faceUp = NO; 
     button.alpha = 1; 
    } 

    self.notificationLabel.text = nil; 
    [self updateUI]; 
} 
+1

http://stackoverflow.com/questions/3826659/uialertview-delegates –

ответ

1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex != [alertView cancelButtonIndex]) 
    { 
     //User clicked ok 
     NSLog(@"ok"); 
     [self newGame:[UIButton alloc] ]; 
    } 
    else 
    { 
     //User clicked cancel 
     NSLog(@"cancel"); 
    } 
} 

значения ButtonIndex слева направо.

+0

спасибо, пожалуйста, ознакомьтесь с обновленным методом в моем вопросе @Sunny – JohnBigs

+0

сохраните этот код, если блок – Balu

+0

один раз увидеть мой ответ, он будет работать. – Balu

0

Используйте метод делегата от UIAlertView в

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1)//Yes 
    { 
     self.flipsCount = 0; 
     self.game = nil; 
     for (UIButton *button in self.cardButtons) 
     { 
      Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:button]]; 
      card.unplayble = NO; 
      card.faceUp = NO; 
      button.alpha = 1; 
     } 

     self.notificationLabel.text = nil; 
     [self updateUI]; 
    } 
    else//No 
    { 
     //do something 
    } 
} 
0

Вставьте следующий метод в одном классе (или в классе вы установили в качестве delegate):

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
    //User clicked ok 
    } 
    else 
    { 
    //User clicked cancel 
    } 
} 
+0

спасибо, пожалуйста, ознакомьтесь с обновленным методом в моем вопросе @Sunkas – JohnBigs

+0

@JohnBigs см. Мой обновленный ответ. – Girish

Смежные вопросы