views:

31

answers:

1

I have having a very odd issue when utilizing this UIAlertView. When viewing a Physician they have several offices. Upon selecting one you get an alert that offers to call this location or display it on a map. To create the alert and to have data at the ready when the alert is dismissed, I declared 4 NSStrings (although I probably only need 2) in the header file. (alertTitle, alertText, alertNumber, and alertAddress)

When looking at the code, the problem is where the alertAddress is involved. Also keep alertNumber in mind. I had a lot of this code condensed but have expanded it to help myself find the problem!

 -(IBAction)address1ButtonPressed:(id) sender {
        Formatter *pnf = [Formatter alloc];
        alertTitle = [physician objectForKey:ADDRESS1DESC_KEY];
        NSString *a = [physician objectForKey:ADDRESS1A_KEY];
        NSString *b =[physician objectForKey:ADDRESS1CITY_KEY];
        NSString *c =[physician objectForKey:ADDRESS1STATE_KEY];
        NSString *d = [physician objectForKey:ADDRESS1ZIP_KEY];
        NSString *p = [physician objectForKey:PHONE1A_KEY];
        alertAddress = [[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@,+%@,+%@+%@",a,b,c,d] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",alertAddress);
        alertText = [NSString stringWithFormat:@"%@\n%@, %@ %@\n%@",a,b,c,d,[pnf stringFromPhoneNumber:p]];
        alertNumber = [p stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [pnf release];
        UIAlertView *phoneAlert = [[UIAlertView alloc] initWithTitle:alertTitle message:alertText delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call",@"View Map",nil];
        [phoneAlert show];

        }

All is well until we reach the point where we handle the alert dismissal. alertNumber seems to come across just fine, I can use it to trigger the phone call and Log it to the console.

alertAddress however is not at all happy about doing the same thing. even trying to Log it to the Console causes a EXC_BAD_ACCESS. alertAddress logs the data correctly before the alert is involved but accessing this data at all when handling the alert button dismissal causes a problem. I have even used the alertNumber it is place and the code functions perfectly.

Why are both exact same NSString variables behaving so differently when used the exact same way?

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSLog(@"Dialing: %@",alertNumber);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",alertNumber]]];      
    }
    if (buttonIndex == 2) {
        NSLog(@"Map Selected");
        NSLog(@"alertAddress contains: %@",alertAddress);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",alertAddress]]];     
    }
}

Here are the related declarations in the header file too...

@interface PhysicianDetailViewController: UIViewController {
    ...
    NSString *alertTitle;
    NSString *alertText;
    NSString *alertNumber;
    NSString *alertAddress;
...
}

@property (nonatomic, retain) NSString *alertTitle;
@property (nonatomic, retain) NSString *alertText;
@property (nonatomic, retain) NSString *alertNumber;
@property (nonatomic, retain) NSString *alertAddress;
...

And here is the console output during this process if it helps....

    > 2010-10-29 11:09:17.954 [2672:307] http://maps.google.com/maps?q=123%20Main%20Street%0ASuite%20A,+Tampa,+FL+11111
    > 2010-10-29 11:09:21.657 [2672:307] Map Selected   
    > Program received signal:  “EXC_BAD_ACCESS”.
    > kill quit
+1  A: 

Use setter so instance will be retained. Remember to release it, when you no longer need it.

self.alertAddress = [[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@,+%@,+%@+%@",a,b,c,d] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Do the same with other properties.

The other thing is that it seems you have memory leak:

UIAlertView *phoneAlert = [[UIAlertView alloc] initWithTitle:alertTitle message:alertText delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call",@"View Map",nil];
[phoneAlert show];
//add release after showing alert
[phoneAlert release]; 
lukewar
I still get confused with using a setter... but it works for sure. Thanks
TheHockeyGeek
I guess I should have submitted my earlier comment as an answer... Anyway, the defined property for the class variable is only used if you use the setter (self.alertAddress = ). Otherwise the variable is set directly, so you would have to manually add a retain to the end (which is somewhat sloppy). Hope this helps
blindJesse