简体   繁体   English

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

[英]UITextField In UIAlert Input Not Working iOS 7

I am currently using the following to use a popup alert which allows the user to set a delay in seconds; 我当前正在使用以下命令使用弹出警报,该警报允许用户设置延迟(以秒为单位);

The input itself works fine, but the number is not remembered, so if I put 5, or 10, when I press 'set' it just ignores that value and goes to instant recording again. 输入本身可以正常工作,但数字不会被记住,因此,如果我输入5或10,则在按“设置”时,它将忽略该值并再次进行即时记录。 here is the IBAction to call the setDelay, and below it, I have posted the Alertview 这是调用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=@"";
        }
    }

}

From iOS 7 you can't add subviews to UIAlertView anymore - UIAlertView addSubview in iOS7 . 从iOS 7开始,您无法再将子视图添加到UIAlertView - iOS7中的UIAlertView addSubview

But if you need just simple UITextField in your UIAlertView you can just set UIAlertView style to UIAlertViewStylePlainTextInput and retrieve value from that field later. 但是,如果您只需要在UIAlertView简单的UITextField ,则可以将UIAlertView样式设置为UIAlertViewStylePlainTextInput然后在该字段中检索值。 Eg. 例如。

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];

then in your delegate method: 然后在您的委托方法中:

 -(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 answer is absolutetly correct. @Guferos的答案是绝对正确的。 It doesn't work because iOS 7 doesnt support this. 它不起作用,因为iOS 7不支持此功能。

But in case you need more than a simple TextView, you can use a framework such as https://github.com/jdg/MBProgressHUD 但是,如果您需要的不仅仅是简单的TextView,则可以使用诸如https://github.com/jdg/MBProgressHUD之类的框架

:) :)

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

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