Search code examples
iphoneuikitopengl-esmultiple-views

Switch between OpenGL ES View and UIKit View controllers


this is my question.

I'm developing a game on iPhone with OpenGL ES. I began my development using the Xcode template OpenGL ES Application, with one EAGLView, etc.

My menu view and game view use the same OGL view but now I want to add score ranking so I need a keyboard and a text field to write name, and some labels or a table view to show ranking.

UPDATE: well, I think that the best solution is to have my current glView with another UIKit View where I will put the score ranking and do the switching.

I have the Apress book Beginning iPhone Development and I follow the chapter about multiple views. I take the point of switching between two UIViewController but when I'm going to make changes in my game... glView is not a UIViewController, it's a UIView, so I'm lost.

Can anyone help me with this? :-(

Thank you very much.


Solution

  • Well, I think I solved the problem, so I share the solution with you.

    I started from the multiple views example from the book: three xib (main window and two views we switch), the app. delegate, two view controller for each view and a root view controller which switch between our views.

    I added the EAGLView class and renderer to this project and changed the initWithCoder method on EAGLView to initWithFrame:

    //- (id) initWithCoder:(NSCoder*)coder
    - (id)initWithFrame:(CGRect)frame
    {    
        if ((self = [super initWithFrame:frame])) {
        // The same we had.
    

    Then, in my root view controller I have added an IBAction when I tap a button:

    - (IBAction)drawEAGLView:(id)sender {
        CGRect rect = [[UIScreen mainScreen] applicationFrame];
        glView = [[EAGLView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
        self.view = glView;
        [glView startAnimation];
    }
    

    And that's all. Very simple and it works well. Maybe it needs some optimization but this is a good start point.

    So, I can make now my score ranking :D