简体   繁体   中英

how to show uialertview in main thread from nsobject class

I tried to show UIAlertView in another thread(Main Thread). But it always goes to thread 1 exc_bad_access (code=1 address=0x0) error.

I Used following code.

if ([urlData writeToFile:attachmentPath atomically:YES]) {
    UIAlertView *sucessAlrt = [[UIAlertView alloc] initWithTitle:gaAppName
                           message:@"File has been Downloaded successfully"
                          delegate:self
                 cancelButtonTitle:nil
                 otherButtonTitles:@"OK", nil];

   [sucessAlrt performSelectorOnMainThread:@selector(show) 
                                withObject:nil waitUntilDone:NO]; 
}  

why it goes to bad access error ??

Have you implemented the UIAlertViewDelegate methods? And more importantly, has this object from which you're calling this stayed within scope during the alert? If it is released, you would receive an exception.

Incidentally, unless you need the app to do something when the user taps on the @"OK" button, you'd pass nil for the delegate . If you need the delegate , make sure the object is retained.

I think when you click "OK" its goes bad access because uialertview is not created in main thread, but you're trying to access the uialertview from main thread, by clicking "OK" when the dialog appears.I misunderstood the question at first, sorry :D So putting the whole code in a gcd call should solve this problem. Instantiate the alertview in main thread, and run your selector in the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM