ios - 检查第二个 UIAlertView 的 buttonIndex

标签 ios objective-c uialertview

我在一个方法中有 2 个 alertView,我想检查第二个 alertView 的按钮索引。我该怎么做?我想获取 else 条件的 buttonIndex。下面是我正在尝试执行的代码。

-(IBAction)buttonClick{

    NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.xyz.com"] encoding:NO error:nil];

    if(connect == NULL) {        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:@"Please check your internet connection" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];        
        [alert show];               
    } else {    
        UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"Upload to the server?" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"CANCEL",nil];    
        [alert1 show];        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        My functionality
}   

最佳答案

一种方法是让警报 View 的实例全局化并在警报 View 的委托(delegate)方法中检查它们:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //  assuming the myAlert2 is your alert's instance
    if (alertView == myAlert2)
    {
        if (buttonIndex == 0)
        {
            ...
        }
    }
 }

或者您可以只给警报添加标签,然后在 alertView 的委托(delegate)方法中检查标签,例如:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        if (buttonIndex == 0)
        { 
            ...
        }
    }
}

关于ios - 检查第二个 UIAlertView 的 buttonIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201242/

相关文章:

ios - 为 iOS 7 和 iOS 8 的 Root View Controller 获取不同的框架

ios - 禁用实时应用程序的 TestFlight 检查点

ios - UILabel 中的 Youtube/Instagram 评论风格

ios - 在多个 View 之一中关闭键盘

html - iOS 不支持 Google 字体 woff2

ios - 检查应用程序是否通过 GameCenter 挑战启动

iphone - 我的 iphone 3gs 应用程序可以在 iphone 4 上正常运行吗?

ios - UIAlertView 在 iOS7 上有可滚动的标题和消息

ios - UIAlertView 的屏幕截图

ios - 如何将 TextField 和 CheckBox 添加到同一个 AlertView