在自定义segue中从一个UIViewController转换到另一个UIViewController

时间:2013-01-21 10:01:44

标签: objective-c ios5 uistoryboardsegue

此自定义segue在视觉上正常工作,但在完成时会出现警告。

Warning: Attempt to present <DestViewController: 0x21059f40> on <SrcViewController: 0x1fd50cf0> whose view is not in the window hierarchy!

任何关于如何在没有警告的情况下使其工作的帮助将非常感激,花费了更多的时间在这上面而不是我应该。甚至不确定我是否应该关心它,因为它是一个警告。

- (void)perform {
    UIViewController *src = (UIViewController *)self.sourceViewController;
    UIViewController *dst = (UIViewController *)self.destinationViewController;

    dst.view.alpha = 0;

    [UIView animateWithDuration:0.5
                 animations:^{
                     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                     [src.view addSubview:dst.view];
                     dst.view.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     [dst.view removeFromSuperview];
                     //need to understand why this throws a warning but still works
                     [src presentViewController:dst animated:NO completion:^(){
                         [src reset];
                     }];
                 }];
}

更新

在src UIViewController上的reset方法中调用了一个MPMoviePlayerController。由于某种原因造成了这个问题,一旦删除这个segue就完美了。请参阅下面的答案,了解segue的最终实施情况。

2 个答案:

答案 0 :(得分:0)

您正在从superview中删除视图,即src.view并在其后显示相同的viewController,因此它显示警告。

试试这个(如果这不起作用告诉我):

[UIView animateWithDuration:0.5
                 animations:^{
                     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                     dst.view.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     [src presentViewController:dst animated:NO completion:^(){
                         [src reset];
                     }];
                 }];
}

答案 1 :(得分:0)

虽然最初的问题是由segue以外的东西引起的,如上所述,我想我会发布完整的工作segue代码给其他可能会在这里结束的代码。此示例中的委托是一个自定义UIViewController容器,其中子UIViewContainers以下列方式添加为子项:

[self addChildViewController:childController_];
[self.view addSubview:childController_.view];

segue.h

#import <UIKit/UIKit.h>

@interface COFSimpleSegue : UIStoryboardSegue

@property (assign) UIViewController *delegate;

@end

segue.m

#import "COFSimpleSegue.h"
#import <QuartzCore/QuartzCore.h>

@implementation COFSimpleSegue

@synthesize delegate = delegate_;

- (void)perform {
    UIViewController *src = (UIViewController *)self.sourceViewController;
    UIViewController *dst = (UIViewController *)self.destinationViewController;

    dst.view.frame = delegate_.view.bounds;
    dst.view.autoresizingMask = delegate_.view.autoresizingMask;

    [src willMoveToParentViewController:nil];

    [delegate_
       transitionFromViewController:src
       toViewController:dst
       duration:0.5f
       options:UIViewAnimationOptionTransitionCrossDissolve
       animations:^(void){}
       completion:^(BOOL finished) {
         [dst didMoveToParentViewController:delegate_];
         [src removeFromParentViewController];
       }
    ];
}

@end