我在故事板中定义了一个带有标识符的 GLKViewController。我用代码将它添加到另一个视图控制器
sceneController = (SceneController*)[[self storyboard] instantiateViewControllerWithIdentifier:@"openglcontroller"];
sceneController.view.frame = CGRectMake(0, 120, 320, 360);
[self addChildViewController:sceneController];
我已经在 GLKViewController 的 viewDidLoad 中定义了
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
GLKView *v = (GLKView *)self.view;
v.delegate = self;
v.context = context;
[EAGLContext setCurrentContext:context];
我验证了这个 viewDidLoad 被调用并且一切都存在。但是 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 没有被调用,也没有 -(void)update
GLKViewController 没有暂停,它的 fps 设置为 30。为什么它不调用委托方法?我在以前的应用程序中作为根视图控制器完成了此操作,这当然可以工作。子视图控制器是 iOS 5 中的一项新功能,没有任何文档表明这不应该工作。我错过了什么?
回答:
缺少什么(以防其他人遇到此问题)。
[self addChildViewController:sceneController];
[[self view] addSubview:[sceneController view]]; <<<<<<<<<<<<<<<<
[sceneController didMoveToParentViewController:self];
由于 GLKViewController 中嵌入了一个视图,因此您需要将其添加为子视图,否则它不会出现在任何地方。哦!