views:

95

answers:

2

I have a variable, editForm, that is used as a pointer to a view controller. Later on, I use a delegate method to release editForm when the user taps the button to close the view.

@implementation EditViewController

EditFormViewController *editForm;

[...]

 (void)openeditform:(NSString*)editId {
    editForm = [[EditFormViewController alloc] initWithNibName:@"EditFormView" bundle:nil editId:editId];

[...]

The problem is, the view will often need to be recreated with a fresh slate. Is there a way to release the controller while keeping the empty pointer ready for future instantiations? Or a way to redeclare the "EditFormViewController *editForm;" statement from within a method?

Edit, here's the full openeditform method:

- (void)openeditform:(NSString*)editId {
    EditFormViewController* editFormController = [[EditFormViewController alloc] initWithNibName:@"EditFormView" bundle:nil editId:editId];
    [editFormController.view setFrame:CGRectMake(0, 0, 640, 460)];
    [self.view addSubview:editFormController.view];
    editFormController.delegate = self;
    [UIView beginAnimations:@"editform view open" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];
    [editFormController.view setFrame:CGRectMake(0, 0, 320, 460)];
    [UIView commitAnimations];
    self.editForm = editFormController;
    [editFormController release];
}

For Jerry Jones, here are the bits relevant to the removal of the view.

EditFormViewController.m:

- (void) closeEditForm {
    [UIView beginAnimations:@"editform view close" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];
    //[UIView setAnimationDelegate:self];
    [self.view removeFromSuperview];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate reloadEditViewTable];
    [UIView commitAnimations];
    [delegate didDismissEditFormView];
}

And the delegate method, which is currently not being used for anything, since I implemented Estarriol's suggestion, which isn't fully doing what I want it to do yet:

- (void)didDismissEditFormView {
    NSLog(@"Fired delegate.");
    //[editForm release];
}
+4  A: 

I believe that your EditFormViewController should not be a variable here, but an instance variable of your EditViewController:

.h file:

@class EditFormViewController;
@interface EditViewController
{
....
EditFormViewController* editform;
...
}

...
@property (nonatomic, retain) EditFormViewController* editForm;
...

In your m file:

@implementation EditViewController
...
-(void)openeditform:(NSString*)editId {
    EditFormViewController* editFormController = [[EditFormViewController alloc] initWithNibName:@"EditFormView" bundle:nil editId:editId];
self.editForm = editFormController;
[editFormController release];
...

That way it would be recreated every time openeditform is fired, and the old instance properly released.

Estarriol
Doesn't work. It ends up loading the same view instance again, with the same values as the one closed. (Or I can release self.editForm and then I can't reinstantiate the view.)
redwall_hp
I've edited my question to show the full openeditform method now that I made the changes.
redwall_hp
Also, it seems to be crashing after the view is opened and closed three times, which seems to point to a memory leak...
redwall_hp
Looks like the problem would be somewhere else, could I possibly see your EditFormViewController's initWithNibName:bundle:editId: and its viewDidLoad?
Estarriol
Sure. Here's a pastebin link, so I don't have to keep changing the original question: http://pastie.org/1113807
redwall_hp
Adding "editingCountdown = nil;" to the closeEditForm method seems to force it to load the new data when the view is reloaded, but it still seems to crash the app if you open and close the view too many times...
redwall_hp
Sorry for delay, I was in the middle of nowhere during past few days. If you still have this problem: I think it's crashing somewhere in the dealloc method of your new controller, but it's just a blind guess. Put a few breakpoints there to see when it is crashing (if it's EXC_BAD_ACCESS). Posting your crash message or stack trace could help. You can also try this method: http://iphonedevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html
Estarriol
+1  A: 

This is sort of hard to understand. You are creating a view controller and displaying it's view. Then when you "dismiss" the view controller, you are removing it's view? Do you sometimes show a new "empty" controller, and sometimes show the same "populated" controller?

You are showing us the creation method, but what happens when it's dismissed? When you say re-instantiate, do you really mean that, or do you just mean show it again?

Jerry Jones
I'm removing the view. I want the controller to be released and reset to its "factory" state so that everything is back to the default the next time the view is created.
redwall_hp
Please add the code you execute after the view is removed.
Jerry Jones