Cannot show UIAlertView with a text field

Go To StackoverFlow.com

0

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;
2012-04-05 22:53
by Richard Knop
There's nothing jumping out at me (and you are using ARC, which should quash most memory management issues). Can you show your UIAlertViewDelegate implementation? Can you post a backtrace - Conrad Shultz 2012-04-05 23:02
What version of iOS are trying to run this on?? The alertViewStyle is only available on iOS 5+. Attempting this on iOS 4.3 or below will crash - jmstone617 2012-04-05 23:03
Although on iOS4 it would probably be "unrecognized selector" for setAlertViewStyle. Is it on the UI thread - SVD 2012-04-05 23:06
Ah, good catch @SVD. I didn't notice the exception is on thread 7, which certainly at least raises the question of inappropriate UIKit use on a background thread - Conrad Shultz 2012-04-06 00:22
@jmstone iOS 5, I am using Xcode 4.2 and Snow Leopard - Richard Knop 2012-04-06 00:36
Sorry @RichardKnop but can you give me a feedback in my answer - ggrana 2012-04-06 01:14
Is ARC enabled - jmstone617 2012-04-06 02:08
@jmstone yes, arc is enable - Richard Knop 2012-04-06 15:05
You should be using the strong attribute for ARC. The retain/strong differential is minor, but still. Is the crash on the self.alert show method call - jmstone617 2012-04-06 15:07


1

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!

2012-04-06 00:17
by ggrana
I am too tired now. I will get back to you tomorrow - Richard Knop 2012-04-06 01:24


0

The EXC_BAD_ACCESS is caused by accessing a released object. To avoid this make your call to UIAlertView kind of modal:

Function body:

-(void)checkSaving
{
    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Do you want to add these results to your database?"
        message:@"\n\n"
        delegate:self
        cancelButtonTitle:@"No"
        otherButtonTitles:@"Save", nil];

    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];

    //this prevent the ARC to clean up :
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    NSDate *d;
    d= (NSDate*)[d init];
    while ([alert isVisible]) 
    {
     [rl runUntilDate:d];

    }
}

Your choice result:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons

    if (buttonIndex == 1)//Save
    {
        //do something

    }
    if (buttonIndex == 0)//NO
    {
        //do something
    }
}

Register the functions in the interface declaration:

@interface yourViewController ()
    -(void)checkSaving
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
@end

To call:

[self checkSaving];

I wish this will help you.

2012-12-08 13:06
by araferna
Please edit your answer and format the code to make it readable - kleopatra 2012-12-08 13:40
Ads