2012-04-22 120 views
0

我发现了一些类似于我的问题,但我仍然没有得到它。如何获得在另一个类中声明的类对象?

我有第一类加载继承CCScene,比我创建一个对象去UIView类,并使用addChild和比我创建另一个UIView类的对象,并使用addSubView。 所以CCScene - >的UIView - >的UIView

声明所述第一对象的:

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]; 

第二对象:

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]; 

所以在gameOverMenu类我的UITextField子视图:

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

,我想使键盘移动视图,有人告诉我,喜欢写东西这在我的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; 
} 

但NSLog显示th gm对象没有框架,没有超类,也没有子视图,我想这意味着我应该在这里使用我的表单对象,但我不知道如何从另一个类中获取它。顺便说一句,我创建了cocos2d项目,所以我必须编写[self viewDidLoad];在AppDelegate中,我没有在RootViewController中实现其他方法。

回答

0

在我看来,在你的RootViewController中,你并没有创建gameOverMenu对象;因为这是RootViewController的ivar,所以nilkeyboardDidShow

我不知道我明白你正在尝试做的,但要记住,从RootViewController的,您可以访问在cocos2d运行当前场景致电:

CCScene* container = [CCDirector sharedDirector].runningScene; 

你的层中唯一的孩子现场的,所以你可以做访问:

[container.children objectAtIndex:0] 

你可以在你的类创建实例变量和存取(属性),使一切更清洁,但我希望这有助于为快速修补你的代码。

+0

感谢帮助伙伴 – Alexander 2012-04-23 19:39:04

相关问题