Question

I have a pretty simple set up in mind, having a mainViewController that has a GLKViewController on top of it. The idea is having my GLKViewController in a box, that take sup 1/3 of the screen, on the mainViewController. This can be seen below:

enter image description here

That white box is my own custom GLKViewController with the follow code:

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

On my mainViewController in the viewDidLoad I simply call boxViewController like this:

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];

which works perfect.

Notice that in my boxViewController.m I had view.context = self.context commented out. If you uncomment it, my application crashes without any error messaging (it breaks with a EXC_BAD_ACCESS in the objc_msgSend assembly code [line 8 to be specific]).

What am I doing incorrectly that when I set the context the application crashes? From all the tutorials I noticed that they have the same set up, except not setting the controller on another controller. Though I don't understand why GLKViewController couldn't be framed on another controller, so I don't think that's the issue.

Was it helpful?

Solution

After a few hours of messing around I found that adding the viewController as a child works:

#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];


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top