【问题标题】:How can I get a class object that is declared in another class?如何获取在另一个类中声明的类对象?
【发布时间】:2012-04-22 20:13:17
【问题描述】:

我发现了一些与我类似的问题,但我仍然没有得到它。

我加载了第一个继承 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 显示 gm 对象没有框架,没有超类,也没有子视图,我想这意味着我应该在这里使用我的表单对象,但我不知道如何从另一个类中获取它。顺便创建了cocos2d项目,所以只好写了[self viewDidLoad];在 AppDelegate 中,我没有在 RootViewController 中实现其他方法。

【问题讨论】:

    标签: objective-c ios cocos2d-iphone


    【解决方案1】:

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

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

    CCScene* container = [CCDirector sharedDirector].runningScene;
    

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

    [container.children objectAtIndex:0]
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      相关资源
      最近更新 更多