when viewController is an container

3241 ワード

起因:
    
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 100, 100);
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)buttonAction:(UIButton *)button
{
   // 
    NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];
//    [self addChildViewController:secondViewController];
    [self.view addSubview:secondViewController.view];
//    [secondViewController didMoveToParentViewController:self];
    
}

firstviewcontrollerにsecondViewControllerを追加する
    [secondviewcontroller.view  removeFromSuperview];の時、プログラムは潰れます
原因分析:
secondviewControllerを追加するとviewの時、
システムはviewcontrollの属性viewの参照カウントを増加し、secondviewcontrollerの参照カウントを減少した.このとき:
    NDTSecondViewController delloc
viewcontrolllerは解放されました
secondviewcontroller.view  removeFromSuperview];の場合、システムはviewの参照カウントを減らし、同時にview controllerの参照カウントを減らし、クラッシュする.
解決策:
viewcontrollerを容器にした時
    [self addChildViewController:secondViewController];
    [self.view addSubview:secondViewController.view];
    [secondViewController didMoveToParentViewController:self];
まずsecondviewcontrollerをコンテナに追加します...
   
secondviewcontrollerが除去されると
//インタフェースの削除
    [self.viewremoveFromSuperview];
    [selfremoveFromParentViewController];
この場合:secondviewControllerは自動的に解放されます
自動的に呼び出されます:[secondView Control:nil];
手動で呼び出す必要はありません.詳細は公式ドキュメントを参照してください.
次のような方法を使用すると、secondView Control dismissの場合、secondView Controlは自動的に解放されます.
 [selfpresentViewController:secondViewControlleranimated:YEScompletion:^{
        NSLog(@"complete");
    }];
[selfdismissViewControllerAnimated:YEScompletion:^{}];
nav push,pop同上
追加:
フルスクリーンのviewcontroller Aがsizeサイズ300のviewControllerを追加したい場合は、上記の方法でも可能であり、viewControllerのライフサイクルに影響を及ぼさない
コードは次のとおりです.
 NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    secondViewController.view.frame =  CGRectMake(0, 0, 500, 500);
//    secondViewController.view.frame = CGRectMake(0, 0, 500, 500);

    [self addChildViewController:secondViewController];
    [self.view addSubview:secondViewController.view];
    [nav didMoveToParentViewController:self];

三、childviewcontroller.view containercontrollerに追加viewのサブビューで、メモリやその他の問題はありませんか?
検証:
    
//インターフェイスの追加
    NDTSecondViewController *secondViewController = [[NDTSecondViewController alloc] init];
    
    secondViewController.view.frame = self.smallView.bounds;
    [self addChildViewController:secondViewController];
    [self.smallView addSubview:secondViewController.view];
    [secondViewController didMoveToParentViewController:self];
答え:ライフサイクルに影響を与えず、メモリルールは上記と一致しています.