Uialertview in Swift, Getting Exc_Bad_Access

UIAlertView in Swift, getting EXC_BAD_ACCESS

Try the following code

let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()

But in iOS 8

UIAlertView is deprecated. So use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. It should be:

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

Check the above code, are you getting same error or not ?

UIAlertController EXC_BAD_ACCESS error - Swift

UIAlertController is available only since iOS 8.0. You should still use UIAlertView instead

EXC_BAD_ACCESS on UIAlertView

Most likely you set the alert view's delegate to self (the view controller). So if the view controller is dismissed before the alert view, the alert view tries to contact its (now reallocated) delegate.

Simply pass nil to the alert view delegate instead of self. This assumes you don't actually need to handle the OK button.

EXC_BAD_ACCESS code 2 on UIAlertView in iOS6

I've got it.
I have the same problem, in my case it seems that the method is thrown from background now (now in ios7, in ios6 UIAlertView was automatically put into the main-thread as @nodepond says -thanks!-)..

try to assure that the method is shown from main thread:

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

Good luck!

UIAlertview exc_bad_access

The UIAlertView could be out of scope by the time the dismissAlert method is called (your checking for alert being nil will prevent this code crashing. There is, however, a better way of implementing this where alert will never be out of scope.

Your class that defines the networkAlert method should implement the <UIAlertViewDelegate> protocol. The code below allows you to intercept the user clicking the 'cancel' button and perform a custom action. The default action of pressing cancel is to close the UIAlertView.

@interface YourClassName : UIViewController <UIAlertViewDelegate> {}

@implementation YourClassName

-(void) networkAlert
{
UIAlertView *netWork = [[UIAlertView alloc] initWithTitle:@"error"
message:@"network has problems"
delegate:self
cancelButtonTitle:@"cancel"
otherButtonTitles:nil];
[netWork show];
}

- (void) alertViewCancel:(UIAlertView*)alertView
{
what ever it is you want to do when the cancel button is pressed here
}

iOS UIAlertView giving EXC_BAD_ACCESS

I tested the following method using sendAsynchronousRequest:queue:completionHandler: as the method to download data from the URL. It seems to work fine, I can get the else clause to fire, either by messing up the URL or making the timeout interval .1 seconds.

    NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error == nil) {
//do whatever with the data. I converted it to a string for testing purposes.
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",text);
}else{
NSLog(@"%@",error.localizedDescription);
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Please check your internet connection and pull down to refresh." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[message show];
}
}];

Showing an UIAlertView causes EXC_BAD_ACCESS

The key problem is that UIKit stuff should be called on the main thread.

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

UIKit Framework Reference

Looking at the docs for NSOperation under setCompletionBlock:

Discussion
The exact execution context for your completion block is not guaranteed but is typically a secondary thread. Therefore, you should not use this block to do any work that requires a very specific execution context. Instead, you should shunt that work to your application’s main thread or to the specific thread that is capable of doing it.

The simplest solution to modify your code is to call the UIKit stuff on the main thread

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]
initWithRequest:request];

[op setCompletionBlock:^(void) {

dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* postSubmitAlertView = [[UIAlertView alloc] init];
postSubmitAlertView.delegate = self;
[postSubmitAlertView addButtonWithTitle:@"Ok"];

if(op.response.statusCode == 200) {

postSubmitAlertView.title = @"Submission Good";
postSubmitAlertView.message = @"GOOD";

} else {

postSubmitAlertView.title = @"Submission Failed.";
postSubmitAlertView.message = @"FAIL";

}

[postSubmitAlertView show];
});

}];
[op start];
}


Related Topics



Leave a reply



Submit