2011-01-30 20 views
0

我有一個NSString,在我的應用程序中設置爲一個值的testString。然後在UIAlertView一次按鍵的響應,我有以下代碼:在UIAlertView的輸出中引用NSString?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSLog(@"String: %@", testString); 
    } 
} 

但對於中小企業的原因,這始終會導致應用程序崩潰。我似乎無法弄清楚爲什麼。

+3

你是如何定義和設置的TestString一個正確的價值?顯示更多您正在使用的代碼,因爲到目前爲止提供的內容沒有任何問題,除了不知道如何創建testString及其值如何。 – WrightsCS 2011-01-30 05:53:36

+0

在AlertView中使用NSString沒有任何問題。就像@WriteCS說的那樣,它必須與你的字符串有關。你說這是「在我的應用程序中設置的值」,但這並沒有告訴我們任何有意義的東西。更多的代碼,更多的代碼。 – 2011-01-30 06:09:46

回答

3

也許你用一種便捷方法創建你的字符串並且不保留它,所以當涉及到alert的clickedButtonAtIndex時它已經被自動釋放。 你應該把它定義爲

@property (nonatomic, retain) NSString *testString; 

在實施:

@synthesize testString; 

在創建:

self.testString = [NSString stringWithFormat:@"%@", @"My test string"]; 
上的dealloc

[testString release];

然後,當你在clickedButtonAtIndex使用它,你可以確保您的TestString有