How we can show UIViewController and UIView by using Cocos2d?

14,147

Solution 1

If you are asking how to attach UIKit views and such to a cocos2d-iphone project, you just have to do it like:

[[[Director sharedDirector] window] addSubview:myView];

Updated to cocos 0.7 and now this is:

[[[Director sharedDirector] openGLView] addSubview:myView];

And in Cocos 0.99:

[[[CCDirector sharedDirector] openGLView] addSubview:myView];

And in Cocos 2.0

[[[CCDirector sharedDirector] view] addSubview:myView];

Solution 2

It's very difficult to answer this question just from the code but I think you need to go back and read up a little on UIKit design and cocos2d programming.

HelloController is a view controller - you cannot 'show' it. A view controller is a class that replies to messages from a view and controls the data it displays from the model.

FlipView is an ImageView which is a subclas of UIView. To have UIKit render this image, you need to add it to another view using [UIView addSubView:...]

Here's what I think you want to do:

  1. The menu item receives a touch event. It signals to:
  2. the view controller which
  3. adds the UIImage to the main view

Like I said though, this is a very general question and I really think you should go back to the documentation and think about your design. The Apple docs are good and there are some good iPhone books on the market now.

Share:
14,147
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am trying to build an iPhone app by using Cocos2d.But i have used four types of classes like bellow-

    @interface MenuScene : Scene {}
    
    @end
    @interface FlipView : UIImageView
    {
        CGPoint startTouchPosition;
        NSString *dirString;
        UIImageView *firstPieceView;   
        UIImageView *secondPieceView;
    
    }
    @end
    
    @interface HelloController : UIViewController
    @end
    
    
    @interface MenuLayer: Layer{
            Todo *todo;
            Menu * menu;
            sqlite3 *database;
            NSMutableArray *todos;
        NSString *dirString;
        CGPoint startTouchPosition;
    }
    @property (nonatomic, retain) NSMutableArray *todos;
    -(void) button1: (id)sender;
    -(void) button2: (id)sender;
    -(void) black_jack: (id)sender;
    @end
    

    but how can i show FlipView and HelloController class through MenuLayer class.

  • srikanth rongali
    srikanth rongali almost 14 years
    What shouldI do to remove myView. When I press a button in present scene I move to next scene. But, the added myView is still appearing in that scene too. Thank You.
  • srikanth rongali
    srikanth rongali almost 14 years
    Sorry I did not check well before asking. I got it. I added the following code in the selector for the button. for (id sv in subviews) { [((UIView *)sv) removeFromSuperview]; [sv release]; }
  • heximal
    heximal over 12 years
    one more question. is it possible to awake UIView from nib?
  • Nik
    Nik over 11 years
    At the moment for version 2 [[[CCDirector sharedDirector] view] addSubview:myView];