0

我想检查用户是否单击“是”,例如我想执行某个操作。

这是我的代码行:

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];
}
4

3 回答 3

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 值。

于 2013-03-12T09:27:53.387 回答
0

使用UIAlertViewas的委托方法

- (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
   }
}
于 2013-03-12T09:21:59.477 回答
0

在同一类(或您设置为的类delegate)中插入以下方法:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0)
  {
    //User clicked ok
  }
  else
  {
    //User clicked cancel
  }
}
于 2013-03-12T09:23:14.010 回答