2010-11-15 6 views
2

Je tire vraiment mes cheveux, ce doit être un problème simple, mais je ne peux tout simplement pas le voir.NSString problème hors de portée?

J'essaye juste d'assigner une valeur à une variable d'un champ de texte.

Dans mon fichier h J'ai

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

Mon fichier 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 ??() 

Répondre

6

Vous devez conserver l'objet que vous attribuez à currentPass:

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

devrais-je libérer le currentPass ou non? – Warrior

+0

Le setter va libérer l'ancienne valeur de 'currentPass'. Vous devez le relâcher à la fin de la course (ou le mettre à «néant»). –

+0

ceci ... tellement fatigué de EXC_BAD_ACCESS –

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