IOS:presentmodalviewcontroller 版本

发布于 2024-12-28 03:03:06 字数 547 浏览 3 评论 0原文

我使用此代码打开视图控制器

self.secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:self.secondViewController animated:YES];

[self.secondViewController release];

,但如果我使用 [self.secondViewController release]; 当我第二次调用此代码时它会崩溃,因为

[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x18a890

如果我不使用它,一切都可以,但是在这种情况下,我什么时候可以释放我的第二个视图控制器? 你能帮助我吗?

I use this code ti open a viewcontroller

self.secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:self.secondViewController animated:YES];

[self.secondViewController release];

but if i use [self.secondViewController release]; when I call this code second time it crash because

[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x18a890

if I don't use it, it it's all ok, but in this situation when can I dealloc my secondviewcontroller?
can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2025-01-04 03:03:06

从您的代码(self.secondViewController)来看,我的理解是您已将 secondViewController 声明为 .h 文件中的变量,并且 @synthesize将其添加到您的 .m 文件中。
如果是这样的话,我宁愿做以下事情

if (self.secondViewController == nil)
    self.secondViewController = [[SecondViewController alloc] initWithNib:@"SecondViewController" bundle:nil];

[自我呈现ModalViewController:self.secondViewController动画:是];

在您的 - (void)dealloc 方法中,我将添加 [self.secondViewController release]; 并在 - (void)viewDidUnload 中添加添加[self setSecondViewController:nil];

上面的代码假设您使用 ARC。
如果您使用 ARC,我将修改我的代码如下:

// 不要在 .h 文件中将 secondaryViewController 声明为变量
// 相反,在故事板中,给它一个标识符,例如 secondaryViewController
// 和 

SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[自我呈现ModalViewController:svc动画:是];
svc = 零;

From your code (self.secondViewController), my understanding is that you have declared secondViewController as a variable in your .h file, and @synthesize'd it in your .m file.
If that's right, I would rather do the followings

if (self.secondViewController == nil)
    self.secondViewController = [[SecondViewController alloc] initWithNib:@"SecondViewController" bundle:nil];

[self presentModalViewController:self.secondViewController animated:YES];

and in your - (void)dealloc method, I would add [self.secondViewController release]; and in - (void)viewDidUnload I would add [self setSecondViewController:nil];.

The above code is with the assumption that you are not using ARC.
If you are using ARC, I would revise my code as follows:

// Do not declare secondViewController as a variable in your .h file
// Instead, in storyboard, give it an identifier, e.g. secondViewController
// and 

SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[self presentModalViewController:svc animated:YES];
svc = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文