NSStrings 和 UIAlertView 的问题

发布于 2024-09-29 23:41:04 字数 3085 浏览 7 评论 0原文

我在使用这个 UIAlertView 时遇到了一个非常奇怪的问题。当看医生时,他们有几个办公室。选择一个后,您会收到一条警报,提示您呼叫该位置或将其显示在地图上。为了创建警报并在警报解除时准备好数据,我在头文件中声明了 4 个 NSString(尽管我可能只需要 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2024-10-06 23:41:04

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

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]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文