Jules
Jules

Reputation: 7776

NSString out of scope problem?

I'm really pulling my hair out, this must be a simple problem, but I just can't see it.

I'm just trying to assign a value to a variable from a text field.

In my h file I have

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

My m file.

@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 ?? ()

Upvotes: 2

Views: 1784

Answers (2)

Manjunath
Manjunath

Reputation: 4555

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

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135558

You need to retain the object you assign to currentPass:

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

Upvotes: 6

Related Questions