2014-03-13 96 views
0

我是一个试图学习iOS开发的python/java程序员。我在开源游戏中工作(出于学习目的 - 代码可以在这里找到:https://github.com/nathanborror/FlapFlap)。我试图以编程方式在用户登陆的重新启动页面上添加一个按钮,如果他失去了游戏。这是我的代码,它不给我任何错误,但该按钮只是没有出现。请注意,我已经注释了在开源中提供的代码。UIButton没有出现

#import "NewGameScene.h" 
#import "GameScene.h" 

@implementation NewGameScene { 
    SKSpriteNode *_button; 
    UIButton *myButton; 
} 

- (id)initWithSize:(CGSize)size 
{ 
    if (self = [super initWithSize:size]) { 
    [self setBackgroundColor:[SKColor colorWithRed:.61 green:.74 blue:.86 alpha:1]]; 


     myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown]; 
     [myButton setTitle:@"Start!" forState:UIControlStateNormal]; 
     myButton.frame = CGRectMake(128, 32, self.size.width/2, self.size.height/10); 
     [self.view addSubview:myButton]; 

     /* 
    _button = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:1 alpha:1] size:CGSizeMake(128, 32)]; 
    [_button setPosition:CGPointMake(self.size.width/2, self.size.height/10)]; 
    [self addChild:_button]; 
     */ 
    } 
    return self; 
} 

-(void) myMethod{ 
    SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4]; 
    GameScene *game = [[GameScene alloc] initWithSize:self.size]; 
    [self.scene.view presentScene:game transition:transition]; 
} 

/* 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4]; 
    GameScene *game = [[GameScene alloc] initWithSize:self.size]; 
    [self.scene.view presentScene:game transition:transition]; 
} 
*/ 

@end 
+0

尝试记录self.size以查看它是否真的有价值。 'NSLog(“%d”,self.size);' – r4id4

+1

不会出现的原因:1)它的大小为零2)其坐标出现在其父视图的可见区域之外3)它的颜色是清晰的颜色4)它有一个颜色,但它的alpha设置为0 5)它被隐藏6)另一个视图被添加到它的顶部7)你的self.view不知何故为零,所以addSubview什么也不做。在你的情况下,检查1)和2)最有可能的。 – Gruntcakes

回答

0

尝试......

myButton = [UIButton alloc]initWithFrame:CGRectMake(128, 32, 100, 100); 
     [myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown]; 
     [myButton setTitle:@"Start!" forState:UIControlStateNormal]; 
     [self.view addSubview:myButton]; 

,如果它出现在你的看法见。