Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

iphone - Do I have to call addSubview after calling addChildViewController?

I'm trying to create a container view controller using iOS5 and new methods like addChildViewController.

Do I have to call addSubview after calling addChildViewController?

Do I have to call removeFromSuperview before calling removeChildViewController?

I don't see anything about this in Apple docs. What do you think?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1) Do I have to call addSubview after calling addChildViewController?

Yes

2) Do I have to call removeFromSuperview before calling removeChildViewController?

Not quite

You should call removeFromParentViewController: instead of removeChildViewController: You should also call willMoveToParentViewController:

For adding / removing, you can refer to this great category :

UIViewController + Container

- (void)containerAddChildViewController:(UIViewController *)childViewController {

    [self addChildViewController:childViewController];
    [self.view addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];

}

- (void)containerRemoveChildViewController:(UIViewController *)childViewController {

    [childViewController willMoveToParentViewController:nil];
    [childViewController.view removeFromSuperview];
    [childViewController removeFromParentViewController];

}

Official resource at developer.apple.com


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...