iOS-UIAlertView outton-otherbutton无法正常工作

iOS-UIAlertView outton-otherbutton无法正常工作,ios,cocoa-touch,Ios,Cocoa Touch,我的应用程序有问题。问题是当我按下AlertView上的取消按钮时。它不显示应该出现在我的输出中的“取消”文本。确认和显示密码按钮工作正常,都显示NSLogs,只有取消按钮不显示。这是我的密码。请对我耐心点,因为我是Xcode新手 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitle

我的应用程序有问题。问题是当我按下AlertView上的取消按钮时。它不显示应该出现在我的输出中的“取消”文本。确认和显示密码按钮工作正常,都显示NSLogs,只有取消按钮不显示。这是我的密码。请对我耐心点,因为我是Xcode新手

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Confirm"])
    {
    UITextField *password = [alertView textFieldAtIndex:0];
    NSLog(@"Password: %@", password.text);

        if (buttonIndex != [alertView cancelButtonIndex])
        {
             NSLog(@"cancel");
        }
        else
        {

            NSLog(@"confirm");


            entries = [[NSMutableArray alloc]init];
            NSString *select = [NSString stringWithFormat:@"SELECT * FROM summary2 WHERE username = '%s' and pass = '%s'",[self.lbUser.text UTF8String],[password.text UTF8String]];
            sqlite3_stmt *statement;
            if (sqlite3_prepare(user, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
            {
                if(sqlite3_step(statement)==SQLITE_ROW)
                    {
                     NSLog(@"database updated");
                    [self updatedatabase];
                    UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was updated successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert3 show];
                    }
                else
                    {

                        NSLog(@"Authentication Failed!");
                        UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Wrong Password! Account was not updated." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert2 show];


                    NSLog(@"fail");
                    }


            }


        }
    }
    else if([title isEqualToString:@"View Password"])
    {
        UITextField *password = [alertView textFieldAtIndex:0];
        NSLog(@"Password: %@", password.text);

        if (buttonIndex != [alertView cancelButtonIndex])
        {
            NSLog(@"cancel");
        }
        else
        {

            NSLog(@"confirm");


            entries = [[NSMutableArray alloc]init];
            NSString *select = [NSString stringWithFormat:@"SELECT * FROM summary2 WHERE username = '%s' and pass = '%s'",[self.lbUser.text UTF8String],[password.text UTF8String]];
            sqlite3_stmt *statement;
            if (sqlite3_prepare(user, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
            {
                if(sqlite3_step(statement)==SQLITE_ROW)
                {
                    NSLog(@"database updated");
                    [self switchbtn];

                }
                else
                {
                     //switch1.on=YES;
                    NSLog(@"Authentication Failed!");
                    UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Wrong Password! Cannot view password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert2 show];


                    NSLog(@"fail");
                }


            }


        }
    }
}

根据您的if,if-else语句,您只需要查找“查看密码”和“确认”按钮。if语句中没有分支来检查cancel按钮。

oh!!我现在明白了!非常感谢!这就是为什么我删除[title isEqualToString:@“Confirm”]条件时它会起作用。谢谢没有注意到这一点。如果(buttonIndex!=[alertView cancelButtonIndex]){NSLog(@“cancel”);}不是您打印的取消按钮,则您的代码将被删除?已解决:)。我忘了我没有添加“if([title IsequalString:@“Cancel”]),这样当用户按下AlertView上的Cancel按钮时,它会打印“Cancel”。谢谢