HowTo initialize an EAGLView.m as a Subview

Member
Posts: 95
Joined: 2009.09
Post: #1
Hi there,

When I started to develop my OpenGL ES Application I just used the "New Project"->OpenGl ES Application".

Now that I made the Core Gameview in OpenGL, I'd like to start it from a Main Menu written in UIKit. So I made a new App using UIkit, UIViewControllers, UINavigationViewControllers etc.

The Problem is that in my OpenGL ES Application the EAGLView.m never gets allocated or initialized in any file, so I don't see how I can do that. If I just start it for itself it runs nicely.
Above "initWithCoder" it just says "//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:"

Which NIB is meant? How can I start it without this nib?

I tried to initialize from my main ViewController using -initWithCoder:nil and add the OpenGL View to the main fullscreen view, but I only see a blank white space (but no error messages, its running nicely but without showing anything).

How can I add my OpenGL Application to my Menu Application?
Quote this message in a reply
Member
Posts: 65
Joined: 2009.01
Post: #2
The process of loading a view in Objective-C/Cocoa Touch is a little convoluted.
Let's say you have an OpenGL class called GameView (.h and .m)...
I would usually create a blank nib file just to instantiate the view and to make the connection between the view controller and its view. I would use the New File wizard in Xcode, choose "View XIB" from the User Interfaces tab and name it GameView.xib. (.xib is xml, .nib is the compiled, binary version of the same file).
Then I open the new nib file in Interface Builder and make sure that it has its File's Owner set to the GameViewController class and its view outlet set to GameView. I also make sure that the view object has its class set to GameView on the Inspector.
Then, in the RootViewController class, I would use this code to create the instance of the GameViewController class:

Code:
-(void)loadGameView {
    if (gameViewController == nil) {
    gameViewController = [[UIViewController alloc] initWithNibName:@"GameView" bundle:nil];
    }

    [self.view addSubview:gameViewController.view];
}

So all the nib (xib) is doing is creating the instance of the GameView class and connecting it to its view controller.
Quote this message in a reply
Member
Posts: 65
Joined: 2009.01
Post: #3
One other thing, when you create a UIView subclass using the template, it creates the stub code for the initWithFrame method but not the initWithCoder method.
Code:
- (id)initWithCoder:(NSCoder*)coder
{    
    if ((self = [super initWithCoder:coder])) {
        //custom init code
}
}
If you are creating your view programmatically, use initWithFrame.
But if you are using the method I described above to instantiate the view object from the nib, then you should use the initWithCoder method (the nib is an archive file so it is "decoded" when it opens).
Hope this helps.
Quote this message in a reply
Member
Posts: 65
Joined: 2009.01
Post: #4
Sorry, I re-read your post and realized that what you are asking is how to programmatically connect a view controller to its view.
This is done in the loadView method of the view controller by creating the instance of the view then setting it to the view controller's view property.

Code:
- (void)loadView {
GameView *view = [[GameView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = view;
[view release];
}

Forget all that stuff about the nib...
Quote this message in a reply
Member
Posts: 95
Joined: 2009.09
Post: #5
Thank you, I did my initialising in the init-method of the Viewcontroller not in loadview.
Another problem seemed to be that I call "[self view].superview.superview.navigationcontroller" out of my Menu-Hierachie and I got the number of ".superview" wrong so I got the wrong NavigationController.
Anyway it works now, thank you for your help!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Howto check whether Ipod is playing music Bersaelor 2 4,113 Jan 2, 2010 09:54 AM
Last Post: Bersaelor
  Howto compile a Maya Plug-in on OS X? webwarrior 4 4,576 Dec 16, 2004 01:19 PM
Last Post: KenD