2

新编辑:

我遇到警告:

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

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

我有两个 ViewController:OneViewController 和 TwoViewController,并继承了 UIStoryboardSegue 类。

故事板是这样的: 在此处输入图像描述

每个视图都包含一个按钮,触发 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。

两个视图控制器中的整个代码如下:

一个视图控制器:

#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

两个视图控制器:

#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 之前隐藏键盘。

现在,如果我这样做:

启动应用程序=>点击“更改为两个”=>点击文本字段以显示键盘=>点击“更改为一个”(然后键盘将隐藏,之后,视图将更改)=>点击“更改为拖车" => 点击文本字段以显示键盘 => 点击 "Change to One"

然后,我收到了警告:

警告:尝试呈现不在窗口层次结构中的视图!

而且,如果我再重复一次,我会收到 2 个警告……再一次,我会收到 3 个……以此类推……

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

谁能帮忙???

4

0 回答 0