我正在使用此代码更新电子邮件地址和忘记密码,但是当我单击“ForgotPassword”按钮时,它们是一个问题,但是当我单击“UpdateEmail”按钮时,它们却无法正常工作,因此它会为“ForgotPassword”按钮调用UIAlert当我按下“UpdateEmail” else if (self.ForgotPassword.tag == 1)时,我试图将其称为“-(Void)alertViewUIButton部分”。

//Forgot method for ForgotPassword

-(IBAction)ForgotPassword:(id)sender

{
       UIAlertView * forgotPassword=[[UIAlertView alloc] initWithTitle:@"Forgot Password"      message:@"Please enter your email id" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

       forgotPassword.alertViewStyle=UIAlertViewStylePlainTextInput;
       [forgotPassword textFieldAtIndex:0].delegate=self;
       [forgotPassword show];
}

//Method for Update Email Address

-(IBAction)UpdateEmail:(id)sender

{
    if ([PFUser currentUser])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Email"
                                                    message:@"Enter Your Email Address"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok",nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
    }
    else
    {
        UIAlertView *myAlert1 = [[UIAlertView alloc]
                                 initWithTitle:@"Please First Loginig"
                                 message:@"Please First Loging"
                                 delegate:nil
                                 cancelButtonTitle:@"Cancel"
                                 otherButtonTitles:@"Ok",nil];
        [myAlert1 show];
    }
}

// Method for Alert View

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    self.ForgotPassword.tag=0;
    self.UpdateEmail.tag=1;

    if (self.ForgotPassword.tag == 0){

        if(buttonIndex ==1){

            NSLog(@"ok button clicked in forgot password alert view");
            NSString *femailId=[alertView textFieldAtIndex:0].text;

            if ([femailId isEqualToString:@""]){

                UIAlertView *display;

                display=[[UIAlertView alloc] initWithTitle:@"Email" message:@"Please enter password for resetting password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
                [display show];
            }else{

                [PFUser requestPasswordResetForEmailInBackground:femailId block:^(BOOL succeeded, NSError *error){

                    UIAlertView *display;

                    if(succeeded){

                        display=[[UIAlertView alloc] initWithTitle:@"Password email" message:@"Please check your email for resetting the password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];

                    }else{

                        display=[[UIAlertView alloc] initWithTitle:@"Email" message:@"Email doesn't exists in our database" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
                    }
                    [display show];
                }];
            }
        }
    }else if (self.ForgotPassword.tag == 1){

        PFUser *user = [PFUser currentUser];
        user[@"email"] = [alertView textFieldAtIndex:0].text;
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){

             if (succeeded){

                 UIAlertView *myAlert1 = [[UIAlertView alloc]
                                          initWithTitle:@"Email Upadated!"
                                          message:@"your Email is Updated"
                                          delegate:nil
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok",nil];
                 [myAlert1 show];
                 //NSLog(@"Success");
             }else{

                 UIAlertView *myAlert1 = [[UIAlertView alloc]
                                          initWithTitle:@"Email is NOT Update"
                                          message:@"Email is alredy registred"
                                          delegate:nil
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok",nil];
                 [myAlert1 show];
                 NSLog(@"Error");
             }
         }];
    }

}

最佳答案

您需要为您的两个不同的UIAlertView提供标签,如下所示。

-(IBAction)ForgotPassword:(id)sender

{

   UIAlertView * forgotPassword=[[UIAlertView alloc] initWithTitle:@"Forgot Password"      message:@"Please enter your email id" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

   forgotPassword.alertViewStyle=UIAlertViewStylePlainTextInput;
   [forgotPassword textFieldAtIndex:0].delegate=self;
   [forgotPassword show];
   forgotPassword.tag = 0; //// Here for forgot password
}

-(IBAction)UpdateEmail:(id)sender

{

    if ([PFUser currentUser])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Email"
                                                message:@"Enter Your Email Address"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Ok",nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alert show];
        alert.tag =1; ///Here for email update
    }
}

然后,在-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex中,您可以检测到单击了哪个alertView's按钮。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
{
    if(alertView.tag == 0)  /// Because we assigned forgotPassword.tag = 0; above for forgotPassword
    {

        if(buttonIndex == YOUR_DESIRED_BUTTON_INDEX)
        {
            ///Your code for Forgot Password.

        }
    }
    else if(alertView.tag ==1) /// Because we assigned alert.tag = 1; above for update email
    {
        if(buttonIndex == YOUR_DESIRED_BUTTON_INDEX)
        {
            ///Your code for Update Email.

        }
    }
}

07-27 19:25