2013-03-13 108 views
3

我有一個非常簡單的設置記住,有一個GLViewViewController上的一個mainViewController。這個想法是讓我的GLKViewController放在一個盒子裏,在mainViewController上佔據屏幕的1/3。這可以看到下面:GLKViewController添加到子視圖 - GLKView上下文導致崩潰

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我只需撥打boxViewController這樣我mainViewController

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彙編代碼中的EXC_BAD_ACCESS [第8行是特定的])。

我做錯了什麼,當我設置上下文應用程序崩潰?從所有的教程中我都注意到他們有相同的設置,除了沒有在其他控制器上設置控制器。雖然我不明白爲什麼GLKViewController不能在另一個控制器上構建,所以我不認爲這是問題所在。

回答

4

亂搞的幾個小時後,我發現,加入的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]; 


} 
+1

我有同樣的問題試圖將AdBannerView添加作爲視圖的孩子。不知道廣告橫幅是否設計爲與控制器配合使用。很明顯,GLKView對沒有控制器的子視圖並不滿意。我在這裏找不到有關確切問題的更多信息。你能獲得更多信息嗎? – Shammi 2014-05-08 14:24:52

+0

嗨@Shammi,我不確定它如何與集成AdBannerView和GLKView一起工作。你可能想問一個關於它的問題,這樣其他人可以提供幫助。 – 2014-05-09 13:07:02