简体   繁体   中英

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. here is the IBAction to call the setDelay, and below it, I have posted the 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 .

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. 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. It doesn't work because iOS 7 doesnt support this.

But in case you need more than a simple TextView, you can use a framework such as https://github.com/jdg/MBProgressHUD

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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