开发者

UIAlertView clickedButtonAtIndex EXC_BAD_ACCESS

开发者 https://www.devze.com 2023-02-28 01:21 出处:网络
I currently have a UIAlertView being shown with two option buttons. When the user presses one of the buttons, I would like a method (in the same object) to be called that would then retrieve a file fr

I currently have a UIAlertView being shown with two option buttons. When the user presses one of the buttons, I would like a method (in the same object) to be called that would then retrieve a file from the web.

I can call the method fine and can call NSLog(), but as soon as I come to use any object variables, I get an EXC_BAD_ACCESS error.

My first thought was it could be a threading issue, so thought calling NSNotificationCenter might solve it, but that too ends in the same error.

Below is the code I've got at the moment. I have tried a few different things (some are commented out) to no avail. The 'dbURL' object is a property of the class. (Edit: below code is not complete)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

//If database update
[[NSNotificationCenter defaultCenter] postNotificationName:@"newdb开发者_高级运维" object:self]; //EXC_BAD_ACCESS happens here

if ([alertView.title isEqualToString: @"Database Update"]){
    switch (buttonIndex) {
        case 0:
            //[self getNewDatabase];
            //[self performSelectorOnMainThread:@selector(getNewDatabase) withObject:nil waitUntilDone:NO];
            //[NSThread detachNewThreadSelector:@selector(getNewDatabase) toTarget:self withObject:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"newdb" object:self];
            break;




// Get a new database file from the server
- (void)getNewDatabase{


    NSLog(@"in database: %@", dbURL);
}

Thanks in advance!


The problem was an object I was calling as not being retained properly and therefore the reference was lost, resulting in an EXC_BAD_ACCESS error

Thanks to Nick Weaver. Using the NSZombieEnabled argument in the build options helped to identify the rogue reference.


You can't compare NSString with == . You need to use isEqualToString:

[alertView.title isEqualToString:@"Database Update"]
0

精彩评论

暂无评论...
验证码 换一张
取 消