gpt4 book ai didi

iphone - 释放 Controller ,但保留空指针?

转载 作者:行者123 更新时间:2023-12-03 17:30:54 26 4
gpt4 key购买 nike

我有一个变量 editForm,用作指向 View Controller 的指针。后来,当用户点击按钮关闭 View 时,我使用委托(delegate)方法来释放 editForm。

@implementation EditViewController

EditFormViewController *editForm;

[...]

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

[...]

问题是, View 通常需要用新的石板重新创建。有没有办法释放 Controller ,同时保持空指针为将来的实例化做好准备?或者重新声明“EditFormViewController *editForm;”的方法来自方法内的语句?

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

- (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,以下是与删除 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];
}

还有委托(delegate)方法,由于我实现了 Estarriol 的建议,目前尚未用于任何用途,但该方法尚未完全实现我想要的功能:

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

最佳答案

我认为你的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 时都会重新创建它,并正确释放旧实例。

关于iphone - 释放 Controller ,但保留空指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3544890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com