iphone - 使用 View Controller 包含, child 失去 parent

标签 iphone ios uiviewcontroller containment

新的 iOS 开发者为 iPhone 编写第一个委托(delegate)应用程序。

模态调用的父 View Controller 有六个 subview Controller ,它们使用父 View 中的分段控件进行转换。我收到错误:

subview Controller 并且在调用时必须有一个共同的父 View Controller -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]

当我按下第二个按钮时,即我可以在父级出现后按下任何按钮并且它起作用。但是下次我按下一个新按钮时,我会遇到上面的崩溃。使用 NSLogs 我已经确定崩溃的原因是当前和新的 child 在按下按钮之间都失去了他们的 parent 。我不知道为什么。父级相关代码如下:

接口(interface)代码:

#import <UIKit/UIKit.h>
#import "WorksheetScrollViewController.h"
#import "Worksheet1ViewController.h"
#import "Worksheet2ViewController.h"
#import "Worksheet3ViewController.h"
#import "Worksheet4ViewController.h"
#import "WorksheetEndViewController.h"


@interface WorkSheetParentViewController : UIViewController
{
IBOutlet UISegmentedControl *segControl;
IBOutlet UIBarButtonItem *doneButton;
IBOutlet UIView *childContainerView;

WorksheetScrollViewController *wStart;
Worksheet1ViewController *w1; 
Worksheet2ViewController *w2;
Worksheet3ViewController *w3;
Worksheet4ViewController *w4;
WorksheetEndViewController *wEnd;

UIViewController *currentVC;
UIViewController *newVC;

}


-(void)initToolbar;
-(IBAction) doneButtonPressed:(id)sender;
-(IBAction) segControlChanged:(id)sender;

-(void)swapViewControllers;

实现代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
wStart = [[WorksheetScrollViewController alloc        ]initWithNibName:@"WorksheetScrollViewController" bundle:nil];

[self addChildViewController:wStart];
wStart.view.frame=childContainerView.bounds;

[childContainerView addSubview:wStart.view];
[wStart didMoveToParentViewController:self];
currentVC = wStart;

}

- (IBAction) segControlChanged:(id)sender

{

NSLog(@"selected tab bar item:%i",segControl.selectedSegmentIndex);
switch (segControl.selectedSegmentIndex) {
    case 0:
        if (!wStart) {
            wStart = [[WorksheetScrollViewController alloc ]initWithNibName:@"WorksheetScrollViewController" bundle:nil];
        }
        newVC = wStart;
        break;

    case 1:
        if (!w1) {
            w1 = [[Worksheet1ViewController alloc ]initWithNibName:@"Worksheet1ViewController" bundle:nil];
        }
        newVC = w1;
        break;

    case 2:
        if (!w2) {
            w2 = [[Worksheet2ViewController alloc ]initWithNibName:@"Worksheet2ViewController" bundle:nil];
        }
        newVC = w2;
       break;

    case 3:
        if (!w3) {
            w3 = [[Worksheet3ViewController alloc ]initWithNibName:@"Worksheet3ViewController" bundle:nil];
        }
        newVC = w3;
        break;

    case 4:
        if (!w4) {
            w4 = [[Worksheet4ViewController alloc ]initWithNibName:@"Worksheet4ViewController" bundle:nil];
        }
        newVC = w4;
        break;

    case 5:
        if (!wEnd) {
            wEnd = [[WorksheetEndViewController alloc ]initWithNibName:@"WorksheetEndViewController" bundle:nil];
        }
        newVC = wEnd;
        break;


    default:
        break;
}
[self  swapViewControllers];


}

-(void)swapViewControllers
{
NSLog(@"top current VC parent = %@",currentVC.parentViewController);
NSLog(@"top new VC parent = %@",newVC.parentViewController);
[currentVC willMoveToParentViewController:nil];
newVC.view.frame=childContainerView.bounds;

[self addChildViewController:newVC];

NSLog(@"current VC parent = %@",currentVC.parentViewController);
NSLog(@"new VC parent = %@",newVC.parentViewController);
[self transitionFromViewController:currentVC
                  toViewController:newVC
                          duration:1.0
                           options:UIViewAnimationOptionTransitionCurlUp
                        animations:nil
                        completion:^(BOOL finished) {
                            [currentVC removeFromParentViewController];
                            [newVC didMoveToParentViewController:self];

                        }];
currentVC = newVC;
NSLog(@"bottom current VC parent = %@",currentVC.parentViewController);
NSLog(@"bottom new VC parent = %@",newVC.parentViewController);

}

按下按钮 1,然后按下按钮 2 的 Nslog 结果:

2013-08-26 14:49:07.245 Alveolar Gas iPhone[556:707] selected tab bar item:1
2013-08-26 14:49:07.251 Alveolar Gas iPhone[556:707] top current VC parent =      <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:49:07.256 Alveolar Gas iPhone[556:707] top new VC parent = (null)
2013-08-26 14:49:07.437 Alveolar Gas iPhone[556:707] current VC parent = <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:49:07.440 Alveolar Gas iPhone[556:707] new VC parent = <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:49:07.460 Alveolar Gas iPhone[556:707] bottom current VC parent = <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:49:07.462 Alveolar Gas iPhone[556:707] bottom new VC parent = <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:49:13.984 Alveolar Gas iPhone[556:707] selected tab bar item:2
2013-08-26 14:49:33.912 Alveolar Gas iPhone[556:707] top current VC parent = (null)
2013-08-26 14:49:50.038 Alveolar Gas iPhone[556:707] top new VC parent = (null)
2013-08-26 14:55:36.149 Alveolar Gas iPhone[556:707] current VC parent = (null)
2013-08-26 14:55:36.153 Alveolar Gas iPhone[556:707] new VC parent = <WorkSheetParentViewController: 0x198da0>
2013-08-26 14:55:36.160 Alveolar Gas iPhone[556:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Children view controllers <Worksheet1ViewController: 0x1ea780> and <Worksheet2ViewController: 0x1403b0> must have a common parent view controller when calling -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]'

最佳答案

我通过移动 currentVC = newVC 来修复它;进入完成 block 。 SwapViewControllers:现在看起来像这样:

-(void)swapViewControllers
{
NSLog(@"top current VC parent = %@",currentVC.parentViewController);
NSLog(@"top new VC parent = %@",newVC.parentViewController);
[currentVC willMoveToParentViewController:nil];
newVC.view.frame=childContainerView.bounds;

[self addChildViewController:newVC];

NSLog(@"current VC parent = %@",currentVC.parentViewController);
NSLog(@"new VC parent = %@",newVC.parentViewController);
[self transitionFromViewController:currentVC
                  toViewController:newVC
                          duration:1.0
                           options:UIViewAnimationOptionTransitionCurlUp
                        animations:nil
                        completion:^(BOOL finished) {
                            [currentVC removeFromParentViewController];
                            [newVC didMoveToParentViewController:self];
                            currentVC = newVC;
                        }];

NSLog(@"bottom current VC parent = %@",currentVC.parentViewController);
NSLog(@"bottom new VC parent = %@",newVC.parentViewController);

}

关于iphone - 使用 View Controller 包含, child 失去 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446270/

相关文章:

ios - 从 SKScene 获取 UIViewController

ios - 有了 MainViewController,如何在 MainVC 中与实际的 UIContainerViews 通信?

ios - 推一个新的viewController(没有segue)给我一个黑屏?

iphone - 应用程序 ID 在 iOS 应用程序中的用途是什么?将其设为字母数字的缺点是什么?

iphone - 访问 NSMutableArray 到没有值的索引

android - 将 flex 移动项目转换为 flex web 项目

ios - 如何在 Swift 中旋转 UIButton?

iphone - 如何更改 UITextField clearButton 的颜色?

ios - Decimal Pad 键盘在 iOS 8 中无法正常工作

ios - 如何将 .MP4 文件等本地文件流式传输到 Chrome Cast?