gpt4 book ai didi

objective-c - 将 GLKViewController 添加到 subview - GLKView 上下文导致崩溃

转载 作者:搜寻专家 更新时间:2023-10-30 20:05:57 24 4
gpt4 key购买 nike

我有一个非常简单的设置,有一个 mainViewController,上面有一个 GLKViewController。这个想法是将我的 GLKViewController 放在一个盒子里,在 mainViewController 上占据屏幕的 1/3。这可以在下面看到:

enter image description here

那个白框是我自己的自定义 GLKViewController,代码如下:

boxViewController.h

//boxViewController.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface boxViewController : GLKViewController
@end

boxViewController.m

//boxViewController.m
#import "boxViewController.m"

@interface boxViewController () { }
@property (strong, nonatomic) EAGLContext *context;
@end

@implementation boxViewController

-(void)viewDidLoad {
[super viewDidLoad];

self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
}

GLKView *view = (GLKView *)self.view;
// view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
}

@end

viewDidLoad 中的 mainViewController 中,我只是像这样调用 boxViewController:

boxViewController* box = [[boxChartViewController alloc] init];
box.view.layer.frame = CGRectMake(10, 50, self.view.frame.size.width-20, self.view.frame.size.height/3);
[self.view addSubview:box.view];

这很完美。

请注意,在我的 boxViewController.m 中,我将 view.context = self.context 注释掉了。如果您取消注释它,我的应用程序会崩溃而不会出现任何错误消息(它会在 objc_msgSend 汇编代码 [具体为第 8 行] 中以 EXC_BAD_ACCESS 中断。

当我设置上下文时应用程序崩溃是我做错了什么?从所有教程中我注意到它们具有相同的设置,只是没有将 Controller 设置在另一个 Controller 上。虽然我不明白为什么 GLKViewController 不能在另一个 Controller 上构建,所以我认为这不是问题所在。

最佳答案

经过几个小时的摸索,我发现将 viewController 添加为子项是可行的:

#import "mainViewController.h"

@implementation mainViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.layer.backgroundColor = [UIColor colorWithRed:242.0f/255.0f green:242.0f/255.0f blue:242.0f/255.0f alpha:1.0].CGColor;

boxViewController* chart = [[boxViewController alloc] init];
chart.view.layer.frame = CGRectMake(10, 50, self.view.frame.size.width-20, self.view.frame.size.height/3);
chart.view.layer.borderColor = [UIColor blackColor].CGColor;
chart.view.layer.borderWidth = 2.0f;
[self addChildViewController:chart];
[self.view addSubview:chart.view];


}

关于objective-c - 将 GLKViewController 添加到 subview - GLKView 上下文导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15390976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com