简体   繁体   English

在iOS 4 SDK上从SuperView删除视图

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

I'm developing an iPhone 3.1.3 app with iOS 4 SDK. 我正在使用iOS 4 SDK开发iPhone 3.1.3应用程序。

I have two ViewControllers, mainViewController and AboutViewController. 我有两个ViewController,mainViewController和AboutViewController。

I use this code to go from mainViewController to AboutViewController (code inside mainViewController.m): 我使用以下代码从mainViewController转到AboutViewController(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): 然后从AboutViewController返回到mainViewController(AboutViewController.m中的代码):

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

When I click on Back Button on AboutViewController, I get an EXC_BAD_ACCESS. 当我在AboutViewController上单击“后退”按钮时,我得到一个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. 我也尝试在[self.view removeFromSuperview]添加一个断点,但是我不能。

Do you know why? 你知道为什么吗?

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): 然后从AboutViewController返回到mainViewController(AboutViewController.m中的代码):

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

Try: 尝试:

[self presentModalViewController:aboutController animated:YES];

To present the view and: 呈现视图和:

[self dismissModalViewControllerAnimated:YES];

To remove the view... 要删除视图...

1) Make aboutController a class level variable 1)使aboutController为类级别的变量

2) Create a delegate method to handle 2)创建一个委托方法来处理

(IBAction) backClicked:(id) sender

3) In implementation of delegate call 3)实现委托通话

 [aboutController.view removeFromSuperView];

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

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.
}

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

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