如何保留UIView控制器(ARC)



这4个文件与这篇文章有关:

FirstViewController有一个按钮(不是在导航栏上,一个单独的按钮),当它被按下时,页面应该卷曲以呈现FilterViewController。

FirstViewController.h

- (IBAction)searchOptions:(id)sender;

FirstViewController.m:

- (IBAction)searchOptions:(id)sender {
    FilterViewController *ctrl = [[FilterViewController alloc] initWithNibName:@"FilterViewController" bundle:nil];
    [UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:nil];
    [self.navigationController pushViewController:ctrl animated:NO];
}

在FilterViewController上它有一些UI的东西,你按下一个按钮,它保存UI的东西然后页面卷回去显示FirstViewController

FilterViewController.h:

- (IBAction)backToMap:(id)sender;

FilterViewController.m:

- (IBAction)backToMap:(id)sender {
    FirstViewController *ctrl = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        [UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
}

这里的问题是UIView的保留。我如何保留UIView?

当我点击FirstViewController上的按钮时,动画工作并显示页面。然而,在FilterViewController上,当我点击按钮时,它崩溃到调试器,错误:

EXC_BAD_ACCESS(代码= 2,地址= 0×8)

在输出控制台中显示:(lldb)

在页面卷起之后,我有一个步进器,当我单击步进器时,我在调试器中得到相同的错误。

更新:我已经跟踪内存位置错误:https://i.stack.imgur.com/Ye0Rm.png

谢谢。

我注意到的一件事是你在推一个视图控制器,然后用语法"back"推另一个视图控制器。这可能是问题所在:导航堆栈就是堆栈。如果你从视图0开始,推视图1,如果你想回到视图0你"弹出"视图1,而不是再次推视图0。

:

- (IBAction)backToMap:(id)sender {
       FirstViewController *ctrl = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        [UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
}

这里的问题是你试图用UIView的transition方法在视图控制器之间制作动画。

根据文档:

fromView
   The starting view for the transition. By default, this view is removed 
from its superview as part of the transition.
toView
   The ending view for the transition. By default, this view is added 
to the superview of fromView as part of the transition.

所以,当你调用这个方法时,你的ViewController的视图被另一个带动画的视图取代,然后在堆栈上放置下一个没有动画的ViewController,所以它看起来很好(但是你的第一个控制器的视图已经被替换了)。

但是当你试图返回一些错误行为发生时-你替换控制器的视图,那将被删除

所以,我想说,我必须做得更小心,有几种不同的方法使viewcontroller之间的自定义转换。

例如,您可以观看下一个解决方案(它与您的解决方案类似)- http://www.vigorouscoding.com/2011/05/custom-uiviewcontroller-transitions/

https://gist.github.com/jeksys/1507490

最新更新