SpokaneDude
SpokaneDude

Reputation: 4974

App is crashing because a method is being deallocated

I have this code that creates 40 UITextFields in a UIPopover; when I try to put a number in one of the textFields, the app crashes with this:

-[ImportTABDelimitedFile respondsToSelector:]: message sent to deallocated instance

This is the code to create the textfields:

//  create the uitextfields for user to match against their input file columns
CGRect nbrFieldRect = CGRectMake(x-20, y, 26.0f, 26.0f);
UITextField *nbrTextField = [[UITextField alloc] initWithFrame:nbrFieldRect];
if(colorYES)
    nbrTextField.backgroundColor = UIColorFromRGB(0xFFF9AF);
else
    nbrTextField.backgroundColor = [UIColor whiteColor];

nbrTextField.enabled = YES;
[nbrTextField setKeyboardType:UIKeyboardTypeNumberPad];
nbrTextField.textColor = [UIColor blackColor];
nbrTextField.tag = tagNumber + 100;  //  range: 170-215
nbrTextField.font = [UIFont systemFontOfSize:12.0f];
nbrTextField.borderStyle = UITextBorderStyleRoundedRect;
nbrTextField.returnKeyType = UIReturnKeyNext;
nbrTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nbrTextField addTarget:self action:@selector(mapUserFields) forControlEvents: UIControlEventEditingDidEnd];
nbrTextField.delegate = self;
[theView addSubview:nbrTextField];

And this is the code for mapUserFields:

-(void)mapUserFields  {

    NSLog(@"mapUserFields: textFieldDidEndEditing");
}

UPDATE #2 I ran Instruments, and it shows that the method is being deallocated, which is causing the crash; this is the image of the Instruments output: enter image description here

Now, the question is: why is the method being deallocated, and how do I fix it?

Upvotes: 0

Views: 93

Answers (3)

SpokaneDude
SpokaneDude

Reputation: 4974

I found the problem (with the help of Michael Dautermann)... I had to make the called method a property like:

iTABFile = [[ImportTABDelimitedFile alloc]init];

Now, it's not released, and works like a champ! Thank you everybody for your help; I really appreciate it. SD

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

You add the delegate when you create the text field and setup its other properties, like this:

UITextField *nbrTextField = [[UITextField alloc] initWithFrame:nbrFieldRect];
nbrTextField.delegate = self;

and implement its delegate method which is called when you press enter key from keyboard:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

The above code you posted looks fine so far, until you post the logs of crash.

EDIT:The crash logs you posted suggest the object (ImportTABDelimitedFile in this case) which is added as observer for UIControlEventEditingDidEnd event of text field is being deallocated.

Can you make sure one thing that you retain popover in your presenting class, by creating a property with retain type and when you dismiss the property set nil to the popover variable. For example like this:

@property (nonatomic, retain) UIPopoverController *popoverController;

and don't forget to initialize and assign this property when you present popover. Set its delegate to self like this:

popoverController.delegate = self;

Don't forget to implement delegate and in the delegate method, do clear the property variable like this:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    self.popoverController = nil; //if you are using non-arc, release it.
}

Upvotes: 2

Tim
Tim

Reputation: 3434

I am guessing a bit here not seeing the rest of the code, but you need to do two things. Firstly, set up the class that is creating all the textFields to conform to the UITextFieldDelegate protocol. I am asssuming the class you have pasted the code above is the one.

This is done in the header file (the .h)

Likewise, when creating your 40-odd textfields, setting:

nbrTextField.delegate = self;

Will have the current class act as the delegate for the UITextFields.

Hope this helps

Upvotes: 0

Related Questions