繁体   English   中英

UIAlert输入中的UITextField无法正常运行iOS 7

[英]UITextField In UIAlert Input Not Working iOS 7

我当前正在使用以下命令使用弹出警报,该警报允许用户设置延迟(以秒为单位);

输入本身可以正常工作,但数字不会被记住,因此,如果我输入5或10,则在按“设置”时,它将忽略该值并再次进行即时记录。 这是调用setDelay的IBAction,在它下面,我发布了Alertview

-(IBAction)setDelay:(id)sender
{
    // open a alert with text field,  OK and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Set delay in seconds" message:@" "
                                                   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Set", nil];





    CGRect frame = CGRectMake(14, 45, 255, 23);
    if(!delayTextField) {
        delayTextField = [[UITextField alloc] initWithFrame:frame];

        delayTextField.borderStyle = UITextBorderStyleBezel;
        delayTextField.textColor = [UIColor blackColor];
        delayTextField.textAlignment = UITextAlignmentLeft;
        delayTextField.font = [UIFont systemFontOfSize:14.0];

        delayTextField.backgroundColor = [UIColor redColor];
        delayTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

        delayTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;  // use the default type input method (entire keyboard)
        delayTextField.returnKeyType = UIReturnKeyDefault;
        delayTextField.delegate = self;
        delayTextField.clearButtonMode = UITextFieldViewModeWhileEditing;   // has a clear 'x' button to the right
    }
    else 
    {
        delayTextField.text=@"";
    }

    alert.delegate=self;
    [alert addSubview:delayTextField];
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];

    [alert show];
    [alert release]; 
}

//

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) 
    {
        if (buttonIndex!=alertView.cancelButtonIndex)
        {
            [self featureButtonPressed];
        }

    }
    else 
    {
        if (buttonIndex==alertView.cancelButtonIndex)
        {
            self.delayTextField.text=@"";
        }
    }

}

从iOS 7开始,您无法再将子视图添加到UIAlertView - iOS7中的UIAlertView addSubview

但是,如果您只需要在UIAlertView简单的UITextField ,则可以将UIAlertView样式设置为UIAlertViewStylePlainTextInput然后在该字段中检索值。 例如。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Set delay in seconds" message:@" "
                                               delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Set", nil];

alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];

然后在您的委托方法中:

 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100) 
{
    if (buttonIndex!=alertView.cancelButtonIndex)
    {
        [self featureButtonPressed];
    }

}
else 
{
    if (buttonIndex==alertView.cancelButtonIndex)
    {
        UITextField *alertViewTextField = [alertView textFieldAtIndex:0];
        //Do something with alertViewTextField.text value
    }
}

}

@Guferos的答案是绝对正确的。 它不起作用,因为iOS 7不支持此功能。

但是,如果您需要的不仅仅是简单的TextView,则可以使用诸如https://github.com/jdg/MBProgressHUD之类的框架

:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM