从 iOS 4 SDK 上的 SuperView 中删除视图

发布于 2024-12-08 22:21:02 字数 880 浏览 0 评论 0原文

我正在使用 iOS 4 SDK 开发 iPhone 3.1.3 应用程序。

我有两个 ViewController,mainViewController 和 AboutViewController。

我使用此代码从 mainViewController 转到 AboutViewController (mainViewController.m 内的代码):

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
        [[AboutViewController alloc] 
         initWithNibName:@"AboutViewController"
                  bundle:nil];
    [self.view addSubview:aboutController.view];

    [aboutController release];    
}

这从 AboutViewController 返回到 mainViewController (AboutViewController.m 内的代码):

- (IBAction) backClicked:(id) sender
{
    [self.view removeFromSuperview];
}

当我单击 AboutViewController 上的“后退”按钮时,我得到一个 EXC_BAD_ACCESS。

我正在使用基于窗口的应用程序模板。

我还尝试在 [self.view removeFromSuperview] 中添加断点,但我不能。

你知道为什么吗?

I'm developing an iPhone 3.1.3 app with iOS 4 SDK.

I have two ViewControllers, mainViewController and AboutViewController.

I use this code to go from mainViewController to AboutViewController (code inside mainViewController.m):

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
        [[AboutViewController alloc] 
         initWithNibName:@"AboutViewController"
                  bundle:nil];
    [self.view addSubview:aboutController.view];

    [aboutController release];    
}

And this to come back from AboutViewController to mainViewController (code inside AboutViewController.m):

- (IBAction) backClicked:(id) sender
{
    [self.view removeFromSuperview];
}

When I click on Back Button on AboutViewController, I get an EXC_BAD_ACCESS.

I'm using a window-based application template.

I've also tried to add a breakpoint in [self.view removeFromSuperview] but I can't.

Do you know why?

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

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

发布评论

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

评论(4

赠我空喜 2024-12-15 22:21:02

这样做:然后

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
        [[AboutViewController alloc] 
         initWithNibName:@"AboutViewController"
                  bundle:nil];
    [self presentModalViewController:aboutController animated:YES];

    [aboutController release];    
}

从 AboutViewController 返回到 mainViewController (AboutViewController.m 内的代码):

- (IBAction) backClicked:(id) sender
{
    [[self parentViewController] dismissModalViewControllerAnimated:YES]
}

Do this instead:

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
        [[AboutViewController alloc] 
         initWithNibName:@"AboutViewController"
                  bundle:nil];
    [self presentModalViewController:aboutController animated:YES];

    [aboutController release];    
}

And this to come back from AboutViewController to mainViewController (code inside AboutViewController.m):

- (IBAction) backClicked:(id) sender
{
    [[self parentViewController] dismissModalViewControllerAnimated:YES]
}
梦言归人 2024-12-15 22:21:02

你得到 EXC_BAD_ACCESS 的原因是因为在将 viewController 的视图添加为子视图后你释放了控制器,因此触摸事件无法看到预期的 viewController 来处理它。

注释掉如下的发布声明,它应该可以工作

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
    [[AboutViewController alloc] 
     initWithNibName:@"AboutViewController"
              bundle:nil];
    [self.view addSubview:aboutController.view];

//[aboutController release]; To avoid leaking consider creating aboutController variable at instance level and releasing it in the dealloc.
}

The reason why you get EXC_BAD_ACCESS is because after adding the view of a viewController as sub view you released the controller, hence the touch event couldn't see the intended viewController to process it.

comment out the release statement like below and it should work

- (IBAction) aboutClicked:(id)sender
{
    AboutViewController* aboutController =
    [[AboutViewController alloc] 
     initWithNibName:@"AboutViewController"
              bundle:nil];
    [self.view addSubview:aboutController.view];

//[aboutController release]; To avoid leaking consider creating aboutController variable at instance level and releasing it in the dealloc.
}
一紙繁鸢 2024-12-15 22:21:02

尝试:

[self presentModalViewController:aboutController animated:YES];

呈现视图并:

[self dismissModalViewControllerAnimated:YES];

删除视图...

Try:

[self presentModalViewController:aboutController animated:YES];

To present the view and:

[self dismissModalViewControllerAnimated:YES];

To remove the view...

肩上的翅膀 2024-12-15 22:21:02

1) 使 aboutController 成为类级别变量

2) 创建一个委托方法来处理

(IBAction) backClicked:(id) sender

3) 在委托调用的实现中

 [aboutController.view removeFromSuperView];

1) Make aboutController a class level variable

2) Create a delegate method to handle

(IBAction) backClicked:(id) sender

3) In implementation of delegate call

 [aboutController.view removeFromSuperView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文