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

2番目のオブジェクト:

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クラスでは、UITextFieldsubViewを取得しました。

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は、gmオブジェクトにフレーム、スーパークラス、サブビューがないことを示しています。つまり、ここでフォームオブジェクトを使用する必要があると思いますが、別のクラスから取得する方法がわかりません。ちなみにcocos2dプロジェクトを作成したので、[selfviewDidLoad]と書く必要がありました。AppDelegateにあり、RootViewControllerに実装されている他のメソッドはありません。

4

1 に答える 1

0

gameOverMenuRootViewControllerでは、オブジェクトを作成していないように思われます。これはRootViewControllerのivarであるため、にありnilますkeyboardDidShow

あなたが何をしようとしているのかわかりませんが、RootViewControllerから、以下を呼び出すことでcocos2dで実行されている現在のシーンにアクセスできることを覚えておいてください。

CCScene* container = [CCDirector sharedDirector].runningScene;

レイヤーはシーンの唯一の子であるため、次のようにしてアクセスできます。

[container.children objectAtIndex:0]

クラスにivarとアクセサー(プロパティ)を作成して、すべてがよりクリーンになるようにすることもできますが、これがコードへの簡単なパッチとして役立つことを願っています。

于 2012-04-22T21:16:20.807 に答える