0

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=@"";
        }
    }

}
user3355723
  • 235
  • 2
  • 11

2 Answers2

0

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
    }
}

}
Community
  • 1
  • 1
Guferos
  • 4,337
  • 5
  • 18
  • 25
  • hmm, using that delagte method throws up an error 'use of undeclared identifier 'alert' – user3355723 Jul 07 '14 at 23:02
  • `change [alert textFieldAtIndex:0];` to `[alertView textFieldAtIndex:0];` – mc01 Jul 07 '14 at 23:10
  • That only then gives another warning unused variable 'alertViewTextField' and it doesn't work in the app either... – user3355723 Jul 07 '14 at 23:16
  • Firstly - warning is not an error. You have a warning because you have to do something with the declared `alertViewTextField`. You can check if text value of the UITextField has been updated with `NSLog(@"%@",alertViewTextField.text)` – Guferos Jul 08 '14 at 07:58
-1

@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

:)

Cutetare
  • 913
  • 8
  • 24
  • Unfortunately it does not - see my above answer to @Guferos , I only need a simple way to retrieve the text value to confirm the delay selected by the user. – user3355723 Jul 07 '14 at 23:06