3

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.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89

1 Answers1

4

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


}
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • 1
    I have the same issue trying to add the AdBannerView as a child of the view. Not sure if the ad banner is designed to work with a controller. It clearly seems like GLKView is not happy with having child views without a controller. I can't find much info on the exact problem here. Were you able to get more info on this? – Shammi May 08 '14 at 14:24
  • Hey @Shammi, I'm unsure how it will work with the integration AdBannerView and GLKView. You might want to ask a question about it so others can help. – John Riselvato May 09 '14 at 13:07