释放控制器,但保持空指针?

时间:2010-08-23 05:13:40

标签: iphone objective-c cocoa

我有一个变量editForm,用作指向视图控制器的指针。稍后,当用户点击按钮关闭视图时,我使用委托方法释放editForm。

@implementation EditViewController

EditFormViewController *editForm;

[...]

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

[...]

问题是,视图通常需要使用新的平板重新创建。有没有办法释放控制器,同时保持空指针为未来的实例化做好准备?或者重新声明“EditFormViewController * editForm”的方法方法中的陈述?

编辑,这是完整的opensitform方法:

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

对于Jerry Jones,这里有与删除视图相关的位。

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

而且委托方法,目前还没有用于任何事情,因为我实施了Estarriol的建议,但这并没有完全做我想做的事情:

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

2 个答案:

答案 0 :(得分:4)

我相信你的EditFormViewController不应该是一个变量,而是EditViewController的一个实例变量:

.h文件:

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

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

在你的m档案中:

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

这样,每次触发opensitform时都会重新创建它,并且正确释放旧实例。

答案 1 :(得分:1)

这有点难以理解。您正在创建一个视图控制器并显示它的视图。然后当你“关闭”视图控制器时,你正在删除它的视图?你有时会显示一个新的“空”控制器,有时会显示相同的“填充”控制器吗?

您正在向我们展示创建方法,但是当它被解雇时会发生什么?当你说重新实例化时,你真的是那个意思吗,或者你只是想再次展示它?