Richard Knop Richard Knop - 3 months ago 16
iOS Question

Cannot show UIAlertView with a text field

This piece of code which is supposed to show an alert window with a text input:

self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];


Causes this error:

Thread 7: Program received signal: "EXC_BAD_ACCESS"


This is how self.alert is defined:

@interface MyClass : NSObject
{
UIAlertView *alert;
id <MyClassDelegate> __unsafe_unretained delegate;
}

@property (nonatomic, retain) UIAlertView *alert;
@property (unsafe_unretained) id <MyClassDelegate> delegate;

Answer

The problem is maybe because of the customize.

I do not know why, but appear to me that the problem is because of the use of threads + customize of your alert.

Can you try to show this alert on the main thread? What happen?

You probably get an error in this line: self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;

What you need to do if yes, is perform this in the main thread.

- (void) yourMethod{
    [self performSelectorOnMainThread:@selector(yourMethod2) withObject:nil waitUntilDone:NO];
}

- (void) yourMethod2{
    self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [self.alert show];
}

Sorry to can't help you more than that, but I do not know exactly what happen, but I already read about issues when editing things to show, in other threads.

Hope it help you!