iOS编辑Uialertview的TextField

时间:2015-04-25 10:13:57

标签: ios objective-c xcode uialertview

代码:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Name your Search" message:@"Save Search as" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] ;
                alertView.tag = 20;
                alertView.delegate = self;
                alertView.cancelButtonIndex = 1;
                alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
                [alertView show];

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if(alertView.tag == 20){
        UITextField *textField = [alertView textFieldAtIndex:0];
        textField.text = @"testing";
        if ([textField.text length] == 0){
            return NO;
        }
    }
     return YES;  
}

我用TEXTINPUT模式创建了一个UIALERVIEW。我确实在textbox中设置了textvalue。当alertview出现时,我不允许编辑textfield值。我的代码出了什么问题?提前感谢

4 个答案:

答案 0 :(得分:2)

添加以下行:textField.userInteractionEnabled = YES;

在此行之后:textField.text = @"testing";

答案 1 :(得分:1)

您需要添加UIAlertView的委托方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     NSString *test = [[alertView textFieldAtIndex:0] text];
}

并删除

textField.text = @"testing";

答案 2 :(得分:1)

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Name your Search" message:@"Save Search as" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] ;
   alertView.tag = 20;
   alertView.delegate = self;
   alertView.cancelButtonIndex = 1;
      alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
   UITextField *textField = [alertView textFieldAtIndex:0];
   textField.text = @"testing";//set textfield text before showing alert
   [alertView show];



- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
 {
if(alertView.tag == 20)
   {
    UITextField *textField = [alertView textFieldAtIndex:0];
    if (textField.text.length == 0)//it is correct way
    {
        return NO;
    }
}
return YES;
}

答案 3 :(得分:0)

除了上述答案之外,请确保您在视图控制器内和视图控制器中符合UITextField委托,将UITextField的委托设置为self。

然后,在视图控制器中,您可以放入响应编辑请求所需的方法。

您还应该在视图控制器中表示alertB.Edgin,didBeginEditing,didEndEditing,shouldEndEditing,shouldReturn和shouldChangeCharactersInRange的alertView.textfield委托方法。