0

I've found some questions similar to mine, but I still didn't get it.

I have the first class loaded that inherits CCScene, than I create an object go UIView class and use addChild and than I create an object of another UIView class and use addSubView. So CCScene -> UIView -> UIView

declaration of the first object:

startLayer.h:

@interface startLayer : CCScene {
@public
    PlayScene *tView;
}

startLayer.m:

tView = [[PlayScene alloc]initWithFrame:CGRectMake(0, 0, 320, 520)];
tView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
CCUIViewWrapper* wrapper = [CCUIViewWrapper wrapperForUIView:tView];
[self addChild:wrapper];

the second object:

PlayScene.h:

@interface PlayScene : UIView {
    @public
    gameOverMenu* gorm ;
}

PlayScene.m:

gorm = [[gameOverMenu alloc]initWithFrame:CGRectMake(0, 0, 320, 520)];
gorm.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
[self addSubview:gorm];

So in gameOverMenu class I got UITextField subView:

UITextField* tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 150, 20)];
[tf setPlaceholder:@"Type your name.."];
[self addSubview:tf];

and I wanted to make keyboard move the View, I was told to write something like this in my RootViewController:

- (void)viewDidLoad {
[super viewDidLoad];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

 }

-(void)keyboardDidShow:(NSNotification*)notification {
    NSDictionary* userInfo = [notification userInfo];
    NSValue* value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
//    NSLog (@"%@", NSStringFromCGRect(gm.frame));
    NSLog(@"%@", gm.superview);
//    NSLog(@"%d", [gm.subviews count]);
    gm.frame = CGRectMake(gm.frame.origin.x, gm.frame.origin.y, gm.frame.size.width,      gm.frame.size.height-keyboardSize.height);
//    NSLog (@"%@", NSStringFromCGRect(gm.frame));//    NSLog(@"%@", gm.superview);

//    NSLog(@"%d", [gm.subviews count]);
}

-(void)keyboarDidHide:(NSNotification*)notification {
    NSDictionary* userInfo = [notification userInfo];
    NSValue* value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;

    gm.frame = CGRectMake(gm.frame.origin.x, gm.frame.origin.y, gm.frame.size.width, gm.frame.size.height+keyboardSize.height);
}

@class gameOverMenu;

@interface RootViewController : UIViewController {
    @public
    gameOverMenu* gm;
}

but NSLog shows that gm object has no frame, no superclass and no subViews, I guess that means that I should use my form object here but I don't know how to get it from another class. I created cocos2d project by the way, so I had to write [self viewDidLoad]; in AppDelegate and I have no other methods implemented in RootViewController.

4

1 回答 1

0

在我看来,在您的 RootViewController 中,您并没有创建gameOverMenu对象;因为这是 RootViewController 的 ivar,所以它nil位于keyboardDidShow.

我不确定我是否理解您要执行的操作,但请记住,从 RootViewController 您可以通过调用以下方法访问在 cocos2d 中运行的当前场景:

CCScene* container = [CCDirector sharedDirector].runningScene;

您的图层是场景的唯一子级,因此您可以通过以下方式访问它:

[container.children objectAtIndex:0]

您可以在您的类中创建 ivars 和访问器(属性),以便一切都更干净,但我希望这有助于快速修补您的代码。

于 2012-04-22T21:16:20.807 回答