gpt4 book ai didi

iphone - 以编程方式加载 UIViewController? (OpenGL ES 2.0)

转载 作者:行者123 更新时间:2023-12-03 20:26:19 28 4
gpt4 key购买 nike

我尝试在没有界面生成器中的 xib 文件的情况下运行 XCode openGL ES 模板。但仍然得到这一行 [(EAGLView *)self.view setContext:context]; [UIView setContext:]:无法识别的选择器发送到实例 0x4b1fac0 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView setContext:]:无法识别的选择器发送到实例 0x4b1fac0

我做错了什么?

主.mm

int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"myAppDelegate");
[pool release];
return retVal;
}

myAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [[myViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = self.viewController;
return YES;
}

myViewController.m

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!aContext) {
aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
}
if (!aContext)
NSLog(@"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(@"Failed to set ES context current");

self.context = aContext;
[aContext release];

[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];

if ([context API] == kEAGLRenderingAPIOpenGLES2)
[self loadShaders];

animating = FALSE;
animationFrameInterval = 1;
self.displayLink = nil;
return 0;
}

我从我的 plist 文件(以及项目中的 xib 文件)以及每个 IBoutlet 中删除了默认的 xib。我错过了什么吗?我猜我的 appDelegate 出了问题 - 但无法弄清楚是什么或为什么。任何帮助将不胜感激。

最佳答案

毫无疑问,您似乎是在标准 UIView 上调用 setContext,而不是在 EAGLView 上。你的 View Controller 可能应该实现 -loadView,并执行如下操作:

- (void)loadView {
self.view = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}

一件看起来很奇怪的事情是,您使用 initWithNibName 而不是 initWithFrame 初始化 View Controller ,这是我希望您在删除 nib/xib 文件时需要做的事情。

这就是我正在做的事情,也许会有所帮助。

(请注意:我有一个自定义 View Controller ,它有一个 UIView 和一个 subview ,即 EAGLView。(我还在努力添加 ADBanner subview )。我没有在 Controller 上调用“init” ,当我尝试将其作为 subview 添加到窗口时,它的 View 就会被加载。)

main.m - 类似。

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
// Create the window programatically:
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

controller = [GLViewController alloc];
[window addSubview:controller.view];
glView = [controller.glView retain];

[window makeKeyAndVisible];

return YES;
}

ViewController.h:

@interface GLViewController : UIViewController <ADBannerViewDelegate>
{
EAGLView *glView;
}
@property(nonatomic, retain) /*IBOutlet*/ EAGLView *glView;
@end

ViewController.m:

- (void)loadView 
{
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}

-(void)viewDidLoad
{
[super viewDidLoad];

glView = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
glView.userInteractionEnabled=YES;
[self.view addSubview:glView];
}

EAGLView.m:

- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;// JPS - WARNING: this does not work on iOS 3.2!!!

self.contentScaleFactor = [UIScreen mainScreen].scale;
CGSize displaySize = [[UIScreen mainScreen]currentMode].size;
CGRect displayRect = [UIScreen mainScreen].bounds;

eaglLayer.frame = displayRect;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys : [NSNumber numberWithBool : NO],
kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8,
kEAGLDrawablePropertyColorFormat, nil];

context = [[EAGLContext alloc] initWithAPI : kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext : context]) {
[self release];
return;
}

glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer *)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

// Set up thd display link to sync rendering with vblank
animating = FALSE;
displayLinkSupported = FALSE;
animationFrameInterval = 1;
displayLink = nil;
animationTimer = nil;

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
displayLinkSupported = TRUE;
}
return self;
}

关于iphone - 以编程方式加载 UIViewController? (OpenGL ES 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4741846/

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