简体   繁体   English

关闭modalViewController iOS5时EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when dismissing modalViewController iOS5

I've been scratching my head for a long time now, trying to solve this problem. 我已经ing了很长时间,试图解决这个问题。 I've searched StackOverflow, and have found people asking for something that resembles my problem like this question and this question , but none of the answers given have helped me. 我搜索了StackOverflow,发现有人在问一些类似我的问题的东西,例如这个问题这个问题 ,但是给出的答案都没有帮助我。

To explain the problem in detail: 详细解释该问题:

  1. I've used Apple's Paging example to enable paging between view controllers 我已经使用Apple的Paging示例来启用视图控制器之间的分页
  2. The View hierachy is as follows: UIWindow -> UIScrollView (MainController) -> UIViewControllers. 视图层次结构如下:UIWindow-> UIScrollView(MainController)-> UIViewControllers。

    I use this code to create a delegate to the Viewcontroller containing the UISCrollview: 我使用以下代码为包含UISCrollview的Viewcontroller创建委托:

     if(page == 0) { ContractsViewController *controller = [viewControllers objectAtIndex:page]; if ((NSNull *) controller == [NSNull null]) { controller = [[ContractsViewController alloc] initWithNibName:@"ContractsView" bundle:nil]; controller.delegate = self; [viewControllers replaceObjectAtIndex:page withObject:controller]; [controller release]; } // add the controller's view to the scroll view if (controller.view.superview == nil) { CGRect frame = scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; controller.view.frame = frame; controller.view.tag = 0; [scrollView addSubview:controller.view]; } } 

The problem then occurs, when I try to present a modalviewcontroller from my ViewController inside the scrollview using the delegate. 然后,当我尝试使用委托从我的ViewController中的ViewController中呈现一个modalviewcontroller时,就会出现问题。 It works a few times, but then gives me a EXC_BAD_ACCESS. 它可以工作几次,但随后给了我EXC_BAD_ACCESS。 I've also tried posting a notification, and creating a listener in the MainController, to present it that way, but still the same problem. 我也尝试过发布通知,并在MainController中创建一个侦听器,以这种方式呈现它,但仍然是同样的问题。

When testing in iOS 4.3 everything works like a charm, but in iOS5 I get the issue. 在iOS 4.3中进行测试时,所有功能都可以正常运行,但是在iOS5中却遇到了问题。

I hope someone can help me get rid of this issue. 我希望有人可以帮助我摆脱这个问题。

Thank you in advance. 先感谢您。

As a start thank you for all your answers! 首先,感谢您的所有回答! I've finally found the solution to the problem. 我终于找到了解决问题的办法。

As mentioned in my question, I used Apple's Pagecontrol sample as a template for my project. 如我的问题所述,我使用Apple的Pagecontrol示例作为我项目的模板。 This template has a rootViewController which has an XIB with a window containing an UIScrollview and a Pagecontrol. 这个模板有一个rootViewController,它具有一个XIB以及一个包含UIScrollview和Pagecontrol的窗口。

The problem happened, when I was trying to present the ModalView from a UIViewController inside the scrollview. 当我尝试从滚动视图内部的UIViewController呈现ModalView时,发生了问题。 I wanted to use the delegate, to present it through the rootViewController, but found out that it was using the UIScrollview to present it. 我想使用委托,通过rootViewController呈现它,但是发现它正在使用UIScrollview呈现它。

The UIScrollView object was apperently not retained, so after a couple of dismisses I hit the EXC_BAD_ACCESS. UIScrollView对象显然未被保留,因此在解雇了几次之后,我点击了EXC_BAD_ACCESS。

I then figured, that i needed an UIView in the rootViewController, and placed it in the bottom viewhierachy in the XIB, and connected it with the view of the rootViewController. 然后我想出了,我需要在rootViewController中放置一个UIView,并将其放在XIB的底部viewhierachy中,并将其与rootViewController的视图连接起来。 After I did that, I could use [delegate present..] and [delegate dismiss..] in the UIViewController without the EXC_BAD_ACCESS. 完成该操作后,无需使用EXC_BAD_ACCESS就可以在UIViewController中使用[delegate present ..]和[delegate dismiss ..]。

Yay! 好极了! :) :)

I think I see your problem.. in the first if you release controller object and then you try to use it in the second if loop because of which you recieve this. 我想我看到了您的问题..在第一个中,如果您释放controller对象,然后在第二个if循环中尝试使用它,因此您可以收到此问题。 release the controller object after both the if loops have passed. 两个if循环都通过后,释放控制器对象。

EDIT : 编辑

try this code instead.. 请尝试使用此代码。

if(page == 0)
{
    ContractsViewController *controller = [viewControllers objectAtIndex:page];

    if ((NSNull *) controller == [NSNull null])
    {
        controller = [[[ContractsViewController alloc] initWithNibName:@"ContractsView" bundle:nil]autorelease];
        controller.delegate = self;
        [viewControllers replaceObjectAtIndex:page withObject:controller];
    }

    // add the controller's view to the scroll view
    if (controller.view.superview == nil)
    {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        controller.view.tag = 0;
        [scrollView addSubview:controller.view];

    }
}

I suggest removing your [controller release]; 我建议删除您的[controller release]; and adding an autorelease: controller = [[[ContractsViewController alloc] initWithNibName:@"ContractsView" bundle:nil] autorelease]; 并添加自动释放: controller = [[[ContractsViewController alloc] initWithNibName:@"ContractsView" bundle:nil] autorelease]; You don't own controller when it's returned from objectAtIndex: , so that way you could have the same behavior for both controllers, no releases. objectAtIndex:返回时,您不拥有controller ,因此对于这两个控制器,您可能会有相同的行为,没有发布。

Try running the code with NSZombiesEnabled. 尝试使用NSZombiesEnabled运行代码。

My guess is that the viewControllers object is either never initialised or is being released prematurely. 我的猜测是viewControllers对象要么从未初始化,要么被过早释放。

Also, there's this if (page == 0) wierdness: 另外, if (page == 0)奇怪,就会出现以下情况:

if(page == 0) // <== only the first page ever gets the init code calling
{
    ContractsViewController *controller = [viewControllers objectAtIndex:page];

    if ((NSNull *) controller == [NSNull null])
    {
        // ** SNIP **

        [viewControllers replaceObjectAtIndex:page   // <<=== always 0
                                   withObject:controller];
        [controller release];
    }

    // ** SNIP **

}

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

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