UIButtons在场景转换后不会失败

时间:2014-07-28 19:18:12

标签: ios objective-c uibutton

嘿所以我创建了两个按钮,当按下时将当前场景转换到下一个场景或主菜单,它们都可以工作,但按下后按钮都不会消失。它们都延续到我不想要的下一个场景。我试过隐藏按钮并从superView中删除它们但都没有工作。下面是代码在按下时发生的事情的样子。

-(IBAction)buttonPressed:(UIButton *)sender
{
    if ([sender isEqual:self.continueButton]) {
        SKTransition* reveal = [SKTransition doorsCloseVerticalWithDuration:0.5];

        SKScene* transition2Scene = [[Transition2Scene alloc] initWithSize:self.size];
        [self.view presentScene:transition2Scene transition: reveal];
        [self.continueButton removeFromSuperview];
        self.continueButton.hidden = YES;
        self.continueButton.enabled = NO;

    }
    else {
        SKTransition* doors = [SKTransition doorsCloseVerticalWithDuration:0.5];

        SKScene* spriteMyScene = [[SpriteMyScene alloc] initWithSize:self.size];

        [self.view presentScene:spriteMyScene transition: doors];
        self.menuButton.hidden = YES;
        self.menuButton.enabled = NO;
        [self.menuButton removeFromSuperview];
    }
}

而且按钮本身的编码是什么样的。

self.continueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.continueButton.frame = CGRectMake(self.frame.size.width/3, self.frame.size.height * 0.5, 120, 70);
[self.continueButton setTitle:@"Continue" forState:UIControlStateNormal];
[self.continueButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.continueButton];

self.menuButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.menuButton.frame = CGRectMake(self.frame.size.width/3, self.frame.size.height * 0.6, 120, 70);
[self.menuButton setTitle:@"Main Menu" forState:UIControlStateNormal];
[self.menuButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuButton];

我环顾四周寻找答案并尝试了一些事情,但是他们没有用,因为我遇到的问题并不完全相同,所以任何帮助都将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

转换到新场景时,SKScene及其内容将从视图中删除。视图(例如按钮)不会被删除。您需要手动删除它们。这是一个例子......

- (IBAction) buttonPressed:(UIButton *)sender
{
    [self.continueButton removeFromSuperview];
    [self.menuButton removeFromSuperview];

    // Find and remove all UIButtons in your view
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            NSLog(@"%@", view);
            [view removeFromSuperview];
        }
    }

    // Verify that the UIButtons were removed
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            NSLog(@"this shouldn't happen: %@", view);
        }
    }

    // The rest of your code

或者,您可以使用SKSpriteNode创建按钮并将其添加到场景中。这是一个例子......

Setting up buttons in SKScene