NSStrings和UIAlertView的问题

时间:2010-10-29 15:36:36

标签: objective-c nsstring declaration exc-bad-access uialertview

使用这个UIAlertView时,我遇到了一个非常奇怪的问题。在查看医生时,他们有几个办公室。选择一个后,您会收到一条警报,提醒您拨打此位置或将其显示在地图上。为了创建警报并在警报解除时准备好数据,我在头文件中声明了4个NSStrings(尽管我可能只需要2个)。 (alertTitle,alertText,alertNumber和alertAddress)

查看代码时,问题在于涉及alertAddress。还要记住alertNumber。我有很多这些代码浓缩但扩展它以帮助自己找到问题!

 -(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];

        }

一切顺利,直到我们达到处理警报解雇的程度。 alertNumber似乎很好,我可以使用它来触发电话并将其记录到控制台。

然而,

alertAddress对于做同样的事情并不高兴。甚至尝试将其记录到控制台会导致EXC_BAD_ACCESS。 alertAddress在涉及警报之前正确记录数据,但在处理警报按钮解除时完全访问此数据会导致问题。我甚至使用了它所在的alertNumber,并且代码功能完美。

为什么两个完全相同的NSString变量在使用完全相同的方式时行为如此不同?

- (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]]];     
    }
}

以下是头文件中的相关声明......

@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;
...

如果它有帮助,这里是控制台输出....

    > 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 个答案:

答案 0 :(得分:1)

使用setter这样的实例将被保留。当你不再需要它时,请记得释放它。

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

对其他属性也一样。

另一件事是你似乎有内存泄漏:

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];