Lau*_*fan 19 objective-c uinavigationcontroller ios
我试图通过使用下一个代码将代码中的子视图控制器添加到故事板中的当前视图控制器:
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];
}
Run Code Online (Sandbox Code Playgroud)
视图似乎至少在模拟器上显示但在控制台中我得到了很多或错误:
<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.
Run Code Online (Sandbox Code Playgroud)
并且相同的描述但不同的错误:
CGContextSetLineWidth,CGContextSetLineJoin,CGContextSetLineCap,CGContextSetMiterLimit,CGContextSetFlatness,CGContextAddPath,CGContextDrawPath,CGContextRestoreGState
所有这些错误都会被记录两次.
有谁知道我做错了什么?
我也阅读了一些帖子,有些人建议在传递数据之前分配和初始化视图控制器,我也试过没有运气.
小智 28
为什么你不尝试这个代码添加视图我认为这个简单易行..
self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self addChildViewController:self.loginView];
[self.loginView.view setFrame:CGRectMake(0.0f, 0.0f, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[self.contentView addSubview:self.loginView.view];
[self.loginView didMoveToParentViewController:self];
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看此链接.
Piy*_*ush 23
要在设计时创建父子容器关系,请在故事板场景中添加容器视图对象,如下图所示.容器视图对象是占位符对象,表示子视图控制器的内容.使用该视图可以根据容器中的其他视图调整子视图的根视图的大小和位置.
使用一个或多个容器视图加载视图控制器时,Interface Builder还会加载与这些视图关联的子视图控制器.必须在父项的同时实例化子项,以便可以创建适当的父子关系.
如果不使用Interface Builder来设置父子容器关系,则必须通过将每个子项添加到容器视图控制器来以编程方式创建这些关系,如向子内容添加子视图控制器中所述.
要以编程方式将子视图控制器合并到您的内容中,请通过执行以下操作在相关视图控制器之间创建父子关系:
addChildViewController:容器视图控制器的方法.此方法告诉UIKit您的容器视图控制器现在正在管理子视图控制器的视图.这是代码.
- (void)displayContentController:(UIViewController *)content {
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self];
}
Run Code Online (Sandbox Code Playgroud)
Apple开发者编程指南中给出了相同示例的更详细说明.
Swift中的解决方案(撰写本文时的Swift 4):
//load the view controller and add as child
storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
loginVC = storyboard.instantiateViewController(withIdentifier: "LOGIN")
addChildViewController(loginVC)
//make sure that the child view controller's view is the right size
loginVC.view.frame = contentView.bounds
contentView.addSubview(loginVC.view)
//you must call this at the end per Apple's documentation
loginVC.didMove(toParentViewController: self)
Run Code Online (Sandbox Code Playgroud)
笔记: