Cómo mostrar un UIAlertView al seleccionar un elemento en UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).

I presume its because my UIAlertView is triggering the picker to close.

How should I have done this?

- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender 
{
    UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
    NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
    sender.text = selText;

    if (NSOrderedSame==[selText compare:@"Custom"])
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Role" 
                                                            message:@"Enter Role Title" 
                                                           delegate:self 
                                                  cancelButtonTitle:@"Cancel" 
                                                  otherButtonTitles:@"Ok", nil];
        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alertView show]; 
    }
}

preguntado el 28 de agosto de 12 a las 11:08

The issue is at 3rd line where you are setting sender.text = selText. This will call your UITextField delegate method every time the text is changed in textfield. -

2 Respuestas

Implement method like

- (IBAction)doSelectDate:(UIDatePicker *)sender 
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Hi" message:@"AlertView is shwoing" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
} 

and connect above method with ValueChanged Event of UIPickerView;

Respondido 28 ago 12, 11:08

I fixed it like this

- (void)pickerView:(UIPickerView *)pickerView  didSelectRow:(NSInteger)row 
                                                inComponent:(NSInteger)component 
{
    NSString *rowString = [serviceTypeArray objectAtIndex:row];

    if ([rowString compare:@"Custom"] == NSOrderedSame)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Role" 
                                                            message:@"Enter Role Title" 
                                                           delegate:self 
                                                  cancelButtonTitle:@"Cancel" 
                                                  otherButtonTitles:@"Ok", nil];
        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
        [alertView show]; 
    }
    else 
    {
        _serviceType.text = rowString;
    }
}

Respondido 31 ago 12, 11:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.