从UIAlertView按钮调用方法

问题描述:

我创建了一个警报视图,要求输入一些密码,但我想知道如何仅在单击“继续”按钮时调用方法?从UIAlertView按钮调用方法

这是我的代码:

-(IBAction)setUserAlert:(id)sender{ 

UIAlertView *setPassword = [[UIAlertView alloc] initWithTitle:@"Insert Password" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; 

    //The password isn´t visible. 
    setPassword.alertViewStyle = UIAlertViewStyleSecureTextInput; 
    [setPassword show]; 

    // Call the method that i want to call only after you click "Continue". 
    [self displayMessage:(UIButton *)sender]; 
     } 
    } 

//启用只有当长度> = 6和密码匹配

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{ 
//Defining the password 
NSString *password = [[NSString alloc]initWithFormat:@"12345678"]; 
UITextField *setPassword = [alertView textFieldAtIndex:0]; 

    if ([setPassword.text length] >= 6 && [setPassword.text isEqualToString:password]){ 
    return YES; 
    } 
return NO; 
} 

//行动我想打电话给该按钮,点击后“继续”

-(IBAction)displayMessage:(UIButton *)sender{ 

    if ([sender.currentTitle isEqualToString:@"YES"]){ 
    _user.text = [[NSString alloc]initWithFormat:@"Hola Mony"]; 
    _imageUser.hidden = YES; 
    _goodUser.hidden = NO; 
    backgound.hidden = YES; 
    yesButton.hidden = YES; 
    noButton.hidden = YES; 
    _userLabel.hidden =YES; 
    }else{ 
    _user.text = [[NSString alloc]initWithFormat:@"You can´t play"]; 
    _goodUser.hidden = YES; 
    _imageUser.hidden = NO; 
    yesButton.hidden = YES; 
    noButton.hidden = YES; 
    _userLabel.hidden =YES; 
    } 
} 

我想在这个里面调用“displayMessage”,但我不知道该怎么做,或者它是否正确。

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
// the user clicked one of the OK/Cancel buttons 
if (buttonIndex == 1){ 
    // call displayMessage, sends and error. 
} 
} 
+0

您必须根据您的要求调用委托方法。 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 您的应用程序是否会崩溃。然后运行一次你的应用程序在调试模式,然后它会知道你的应用程序崩溃的委托方法或displaymessage方法 – 2012-01-02 07:48:14

+0

我修复它,忘记“displayMessage”方法,但可能不是最好的方式来做到这一点。 - (IBAction)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //用户点击确定/取消按钮 if(buttonIndex == 1){ _user.text = [[NSString alloc ] initWithFormat:@“Hola Mony”]; _imageUser.hidden = YES; _goodUser.hidden = NO; backgound.hidden = YES; yesButton.hidden = YES; noButton.hidden = YES; _userLabel.hidden = YES; } } – unixeO 2012-01-02 08:14:44

如果委托是自我,添加alertView:clickedButtonAtIndex:方法,并确定“继续”是由指数压制。