Question

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"......" message:@"......" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"OK", nil];

[alert show];

I am showing alert view to add a new category using through text field of alert view, when user tapes ok button of alert then I first check that if user has entered anything or not, if not then I show another alert with some message to let user know that text field is mandatory, but previous alert for adding new category disappears.

I want that alert to stay on the screen. So what Should I do ??

This is the crash report and code::

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Vehicle Category" message:@"this gets covered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"OK", nil];
            alert.tag = 5;

txtAddVehicleCategory = [[UITextField alloc]initWithFrame:CGRectMake(12, 45, 260, 25)];

[txtAddVehicleCategory setBackgroundColor:[UIColor whiteColor]];

txtAddVehicleCategory.placeholder = @"Enter Vehicle Category";

txtAddVehicleCategory.contentVerticalAlignment =       UIControlContentVerticalAlignmentCenter;

[alert addSubview:txtAddVehicleCategory];


CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, -50);

[alert setTransform:myTransform];

[alert show];

'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'

Was it helpful?

Solution

When you want to implement some rules for entering text in UIAlertView, you should disable the OK button of your alert view until user follows the rules. You can achieve this with UIAlertViewDelegate methods. Look into alertViewShouldEnableFirstOtherButton:.

Return NO from the method until rule is followed

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    // When return NO, OK button is disabled.
    // When return YES, rule is followed and OK button gets enabled.
    return ([[[alertView textFieldAtIndex:0] text] length]>0)?YES:NO;
}

EDIT

The code that I provided was assuming you are using default alert view style textfield, hence the crash.

You should not addSubview to UIAlertView, because the view hierarchy of alert view is private. Most recent iOS7 will not display any subviews that user adds on UIAlertView.So I recommend you do not do this.

From Apple docs,

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Instead, use UIAlertViewStyle.

Following are the styles supported,

typedef enum {
   UIAlertViewStyleDefault = 0,
   UIAlertViewStyleSecureTextInput,
   UIAlertViewStylePlainTextInput,
   UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

Use the one that suits your requirement. To validate the input entered by the user use the above method that I suggested. Refer this simple guide/tutorial for using the alert view with these styles.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top