新编辑:

我遇到警告:

尝试在视图不在窗口层次结构中的另一个上呈现OneView .

以下是我为显示问题而创建的示例代码 .

我有两个ViewController:OneViewController和TwoViewController,并且子类化了UIStoryboardSegue类 .

故事板是这样的:
enter image description here

每个视图都包含一个按钮,触发segue移动到另一个 .

我的客户segue的代码如下:

- (void)perform {
    UIViewController *sourceVC = self.sourceViewController;
    UIViewController *destinationVC = self.destinationViewController;

    [sourceVC.view addSubview:destinationVC.view];
    [destinationVC.view setFrame:sourceVC.view.frame];
    [destinationVC.view setTransform:CGAffineTransformMakeTranslation(320, 0)];
    [destinationVC.view setAlpha:1.0];

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionTransitionFlipFromRight
                     animations:^{
                         [destinationVC.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
                         [destinationVC.view setAlpha:1.0];
                     }
                     completion:^(BOOL finished){
                         [destinationVC.view removeFromSuperview];
                         [sourceVC presentViewController:destinationVC animated:NO completion:nil];
                     }];
}

即,我想将destinationView从右移动到中心 .

在TwoViewController中,我还有一个UITextField .

两个viewcontroller中的整个代码如下:

OneViewController:

#import "OneViewController.h"

@interface OneViewController ()

@end

@implementation OneViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnTapped:(id)sender {
    [self performSegueWithIdentifier:@"One2TwoSegue" sender:self];
}

@end

TwoViewController:

#import "TwoViewController.h"

@interface TwoViewController ()

- (void)keyboardDidHide:(NSNotification*)notification;

@end

@implementation TwoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnTapped:(id)sender {
    [_inputBox resignFirstResponder];
}

- (void)keyboardDidHide:(NSNotification *)notification {
    [self performSegueWithIdentifier:@"Two2OneSegue" sender:self];
}

@end

在TwoViewController中,我这样写的原因是因为我想在执行segue之前隐藏键盘 .

现在,如果我这样做:

启动app =>点击“更改为两个”=>点击文本字段以显示键盘=>点击“更改为一个”(然后键盘将隐藏,之后,视图将更改)=>点击“更改为拖动“=>点击文本字段以显示键盘=>点按”更改为一个“

然后,我得到了警告:

警告:尝试显示其视图不在窗口层次结构中!

而且,如果我再一次重复,我会得到2个警告......再一次,我会得到3 ......等等......

另外,如果我删除文本字段,只需两个按钮,一切都很好 .

谁能帮忙???