开发者

Release controller, but keep empty pointer?

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


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜