简体   繁体   English

如何在两个视图之间的过渡中处理旧控制器?

[英]How to dispose of old controller in a transition between two views?

I'm trying to create a transition between two views by using their controllers. 我正在尝试使用它们的控制器在两个视图之间创建一个过渡。 Problem is, how to dispose of the old controller once the transition animation is finished? 问题是,过渡动画完成后如何处置旧控制器? I can't dispose of it before the animation is finished as that leads to a segmentation fault - apparently the animations involved do not increment the retain count on the views they work with. 我无法在动画完成之前处理它,因为这会导致分割错误-显然,所涉及的动画不会增加其使用的视图的保留计数。

Only thing I can think of is to provide a method to be called once the animation is finished to release the controller argument. 我唯一能想到的就是提供一种在动画结束后释放控制器参数就可以调用的方法。 I am hoping someone will have a more elegant solution. 我希望有人能有一个更优雅的解决方案。

Here is my sample code for a method in my root view controller: 这是我的根视图控制器中的方法的示例代码:

-(void) switchToController:(UIViewController *) controller {
    [controller viewWillAppear:YES];

    if (self.currentScreenController != nil) {
        [self.currentScreenController viewWillDisappear:YES];
        [[self.currentScreenController view] removeFromSuperview];
    }

    [self.view addSubview:[controller view]];

    if (self.currentScreenController != nil) {
        [self.currentScreenController viewDidDisappear:YES];
    }

    self.currentScreenController = controller;

    [controller viewDidAppear:YES];

    //[controller release]; // <- releasing now will cause errors as subsequent animations reference this controller's view
    ...
    // animations start now

[Edit] Well, I can't seem to be allowed to release the controller even after the animation is finished for some unexplained reason. [编辑]好吧,即使由于某些无法解释的原因动画完成后,我似乎也不允许释放控制器。 I schedule an NSTimer call a few seconds after the animation completes, I check that the retain count is 1 before releasing, and then when I release it I get a crash. 我安排动画完成后几秒钟进行一次NSTimer调用,在释放之前检查保留计数是否为1,然后在释放它时发生崩溃。 Here is the method that is called by the timer to release already unused controllers: 这是计时器调用以释放已经未使用的控制器的方法:

- (void) releaseOldController {
    @synchronized(arrayOfOldControllers) {
    if (2 < [arrayOfOldControllers count]) {
        NSObject * object = [arrayOfOldControllers objectAtIndex:0];
        [arrayOfOldControllers removeObjectAtIndex:0];
        NSLog(@"releasing object (%@) with retain count(%d)", object, [object retainCount]);
        [object release];
    }
}

} }

Perhaps my strategy is wrong. 也许我的策略是错误的。 What I am trying to accomplish is to simulate book page turning. 我想要完成的是模拟书本翻页。 The book has more than 30 pages, I want to dispose of the old page as soon as the transition to the new page is finished to release memory. 这本书有30多页,我想在完成向新页的转换后释放旧页以释放内存。 So when the user turns to a new page, a new controller with a new view is created and added to the root controller with a fade animation transition of about 1/2 sec. 因此,当用户转到新页面时,将创建具有新视图的新控制器,并以大约1/2秒的淡入淡出动画过渡将其添加到根控制器中。 As soon as the animation is finished I want to release the previous page view as only the new page view is visible. 动画完成后,我想释放上一个页面视图,因为只有新页面视图可见。 This way the root controller view has at most 2 subviews at any one time, and usually only 1 -- the current page viewed, after the transition is complete. 这样,根控制器视图在任何时候最多具有2个子视图,通常只有1个子视图-转换完成后,当前页面仅被查看。 This should not be so hard to do, but I don't understand why I am getting an error when releasing a controller when it's no longer in use. 这不应该那么难,但是我不明白为什么释放不再使用的控制器时会出现错误。

[Update] This question is wrong -- the problem is somewhere else and has nothing to do with controller or view. [更新]这个问题是错误的-问题出在其他地方,与控制器或视图无关。 The code above is correct. 上面的代码是正确的。

Providing an animation finished callback method will certainly work, but it's a little annoying. 提供动画完成的回调方法当然可以,但是有点烦人。

A nicer technique available to you: if you're targetting iOS4 and onwards only, you can use animation blocks -- please see iPhone UIView Animation Best Practice -- in particular, the completion: bit allows you to specify what happens at the end of the animation, without having to define a new callback method in the usual way. 一项更好的技术可供您使用:如果仅针对iOS4及更高版本,则可以使用动画块-请参阅iPhone UIView动画最佳做法 -特别是, completion:位可让您指定在结束时发生的情况动画,而不必以通常的方式定义新的回调方法。

If you are trying to simulate the page turning I would load all my controllers in to NSMutableArray and use it as my page guide. 如果您尝试模拟翻页,我会将所有控制器加载到NSMutableArray中,并将其用作页面指南。 You can release all the UIViewControllers after adding them to the array, and then you will be able still have access to them whenever you want to go back. 您可以在将所有UIViewController添加到数组后释放它们,然后,只要您想返回,便仍然可以访问它们。

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

相关问题 如何为视图之间的过渡设置动画? - how to animate transition between views? 由rootView控制器呈现的两个viewController之间的过渡 - Transition between two viewControllers presented by rootView controller 如何为两个视图之间的过渡设置动画,使其看起来像第一个视图向上,而第二个视图则从底部出现? - How to animate transition between two views so it look like the first is going up, and the second appears from bottom? 视图之间的UIView动画过渡 - UIView animation transition between views MultipleDetailViews - 视图之间的动画过渡 - MultipleDetailViews - Animate Transition Between Views 在我的视图控制器中的两个选择器视图之间进行通信 - Communicating between two picker views in my view controller 两个 UINavigationController 之间的转换 - Transition between two UINavigationControllers 1个视图控制器中的两个视图 - Two Views in 1 view controller 如何以编程方式启动/处理iPhone的视图? - How to programmatically launch/dispose of views for the iPhone? 在第一个选项卡的两个视图之间切换时从背景中的第二个选项卡进行的视图(而不是应有的颜色) - View from the second tab in the background at transition between two views at the first tab (and not a color as is should)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM