繁体   English   中英

在iOS 4 SDK上从SuperView删除视图

[英]Removing a view from a SuperView on iOS 4 SDK

我正在使用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]添加一个断点,但是我不能。

你知道为什么吗?

改为这样做:

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

尝试:

[self presentModalViewController:aboutController animated:YES];

呈现视图和:

[self dismissModalViewControllerAnimated:YES];

要删除视图...

1)使aboutController为类级别的变量

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

(IBAction) backClicked:(id) sender

3)实现委托通话

 [aboutController.view removeFromSuperView];

之所以得到EXC_BAD_ACCESS,是因为在将viewController的视图添加为子视图之后,您释放了控制器,因此touch事件无法看到要处理的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.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM