将子视图控制器添加到当前视图控制器 [英] Add child view controller to current view controller
问题描述
我正在尝试使用下一个代码将代码中的子视图控制器添加到故事板中的当前视图控制器:
I am trying to add a child view controller in code, to the current view controller from storyboard by using the next code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
LogInTutorialViewController *lvc = [[LogInTutorialViewController alloc] init];
lvc = (LogInTutorialViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LogInTutorialViewControllerID"];
[self displayContentController:lvc];
- (void) displayContentController: (LogInTutorialViewController*) content;
{
//add as childViewController
[self addChildViewController:content];
[content didMoveToParentViewController:self];
[content.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[self.view addSubview:content.view];
}
视图似乎至少在模拟器上显示但是在控制台中我得到了很多或错误:
The view seem to be displaying on the simulator at least but in console I get a lot or error:
<Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
并且描述相同但错误不同:
And also the same description but different error:
CGContextSetLineWidth,CGContextSetLineJoin,CGContextSetLineCap,CGContextSetMiterLimit,CGContextSetFlatness,CGContextAddPath,CGContextDrawPath,CGContextRestoreGState
CGContextSetLineWidth, CGContextSetLineJoin, CGContextSetLineCap, CGContextSetMiterLimit, CGContextSetFlatness, CGContextAddPath, CGContextDrawPath, CGContextRestoreGState
所有这些错误都会被记录两次。
all these error get logged twice.
有谁知道我做错了什么?
Does anyone know what I am doing wrong?
我也读了一些帖子,其中有些是建议在传递数据之前分配和初始化视图控制器,我也试过没有运气。
also I read a few posts and in some it was suggested to alloc and init the view controller before passing the data, I also tried that without any luck.
推荐答案
didMoveToParentViewController必须是最后。
didMoveToParentViewController must be the last.
这篇关于将子视图控制器添加到当前视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!