0

當使用這個UIAlertView時,我有一個很奇怪的問題。在看醫生時,他們有幾個辦公室。選擇一個後,你會看到一個警報,可以調用這個位置或者在地圖上顯示它。要創建警報並在警報解除時準備好數據,我在頭文件中聲明瞭4個NSString(儘管我可能只需要2個)。 (alertTitle,alertText,alertNumber和alertAddress)NSString和UIAlertView有問題

查看代碼時,問題是涉及到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 
+0

請問,如果你使用的存取工作的呢?只是讓alertAddress被設置爲一個自動發佈的變種。 – blindjesse 2010-10-29 15:45:41

+0

不應該像頭文件中指定的那樣保留它嗎?當我在那裏發佈它時,Dealloc不會給我任何問題。所以我不認爲它被自動釋放。即使我要把alertAddress = @「BLEH」;當通過按下/關閉提醒按鈕進行訪問時,它仍然失敗。 – TheHockeyGeek 2010-10-29 15:53:43

回答

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

我仍然對使用二傳手感到困惑......但它確實有效。謝謝 – TheHockeyGeek 2010-10-30 03:33:00

+0

我想我應該提交我以前的評論作爲答案......無論如何,只有在使用setter(self.alertAddress =)時纔會使用類變量的定義屬性。否則,該變量是直接設置的,所以你將不得不手動添加一個保留到最後(這是有點草率)。希望這可以幫助 – blindjesse 2010-10-30 03:49:30