-2
-(void) alertView: (UIAlertView *) alertVw clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *str = [NSString stringWithFormat:@"%d 。", buttonIndex];
    UIAlertView *newAlertVw = [[UIAlertView alloc] initWithTitle:@"INFO" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [newAlertVw show];
}


UIAlertView *alertVw = [[UIAlertView alloc] initWithTitle:@"TITLE"
                                                      message:@"box message"
                                                     delegate:self
                                            cancelButtonTitle:@"hide"
                                            otherButtonTitles:@"T1", @"T2", nil];
    [alertVw show];

如果用户触摸了其他按钮之一,是的,我们知道用户触摸了哪个按钮。那么,如何获取用户刚刚触摸的按钮的标题?谢谢。

4

3 回答 3

4

UIAlertView使用您设置的委托,self您可以使用它:

-(void)alertView:(UIAlertView*)alertView didDismissWithButtonAtIndex:(NSInteger)buttonIndex
{
  NSLog(@"Button Title -----> %@",[alertView buttonTitleAtIndex:buttonIndex]);

  // or you can check if it equals to string
  if([[alertView buttonTitleAtIndex:buttonIndex]isEqual:@"Enter"])
  {
    // your code goes here
  }
}
于 2014-05-08T15:52:47.197 回答
1

方法是buttonTitleAtIndex:,信息可以在 UIAlertView 文档页面上找到。

https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/buttonTitleAtIndex

我建议您尽可能多地参考文档,这是您学习和记住的方式。

于 2014-05-08T15:59:41.350 回答
0

实际上,如果你想使用委托方法,你必须使用-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex.

并不是

-(void)alertView:(UIAlertView*)alertView didDismissWithButton At Index:(NSInteger)buttonIndex

请参阅:https ://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:didDismissWithButtonIndex :

我猜这只是潘乔的错字。

于 2014-07-30T12:28:46.967 回答