2010-11-15 50 views
2

我真的把我的頭髮拉出來,這一定是一個簡單的問題,但我看不到它。NSString超範圍問題?

我只是試圖從文本字段中賦值給變量。

在我的.h文件我有

NSString *currentPass; 
@property (nonatomic, retain) NSString *currentPass; 

我的M檔。

@synthesize currentPass; 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:  
    (NSInteger)buttonIndex{ 

if (alertView.tag == AlertPasswordAsk) { 
    UITextField* theTextField = ((UITextField*)[alertView viewWithTag: 5]); 
    currentPass = [NSString stringWithFormat:@"%@", theTextField.text]; 
    if ([theTextField isEditing]) { 
     [theTextField resignFirstResponder]; 
    } 
} 
} 

- (void)alertView:(UIAlertView *)alertView 
    didDismissWithButtonIndex:(NSInteger)buttonIndex{ 

    NSLog(@"didDismissWithButtonIndex tag=%i", alertView.tag); 
if (alertView.tag == AlertPasswordAsk) { 
    if(buttonIndex == 1){ 
     NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults]; 
     NSString *strPassword = [NSString alloc]; 
     strPassword = [myDefaults stringForKey:@"pass"]; 

      // ######### ERROR OCCURS HERE ######### 
     NSLog(@"currentPass=%@ strPassword=%@", currentPass, strPassword); 

     if (![currentPass isEqualToString:strPassword]) { 

alt text

[6337:207] didDismissWithButtonIndex tag=10 
Current language: auto; currently objective-c 
(gdb) continue 
Program received signal: 「EXC_BAD_ACCESS」. 
(gdb) bt 
#0 0x02894903 in objc_msgSend() 
#1 0x00000000 in ??() 

回答

6

您需要保留分配給currentPass對象:

self.currentPass = [NSString stringWithFormat:@"%@", theTextField.text]; 
+0

我應該釋放currentPass與否? – Warrior 2011-02-02 08:07:04

+0

setter將釋放'currentPass'的前一個值。您需要在運行結束時釋放它(或將其設置爲「nil」)。 – 2011-02-02 10:45:26

+0

這個......非常厭倦了EXC_BAD_ACCESS – 2011-05-30 11:52:56

1
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:  
    (NSInteger)buttonIndex{ 

if (alertView.tag == AlertPasswordAsk) { 
    UITextField* theTextField = ((UITextField*)[alertView viewWithTag: 5]); 
//assigning to member variable will not retain your object. 
// current address is just pointing to auto released object not retaining it. 
// currentPass = [NSString stringWithFormat:@"%@", theTextField.text]; 

// use currentPass as with accessor: 
self.currentPass = [NSString stringWithFormat:@"%@", theTextField.text]; 
    if ([theTextField isEditing]) { 
     [theTextField resignFirstResponder]; 
    } 
} 
}