我正在 IOS 7 中为游戏切换视图,但无法使此代码正常工作。有啥建议么?

Posted

技术标签:

【中文标题】我正在 IOS 7 中为游戏切换视图,但无法使此代码正常工作。有啥建议么?【英文标题】:I am switching views in IOS 7 for a game and cannot get this code to work. Any suggestions?我正在 IOS 7 中为游戏切换视图,但无法使此代码正常工作。有什么建议么? 【发布时间】:2014-04-01 17:01:12 【问题描述】:

这是我目前使用的代码。如果你有什么需要澄清的,我会尽快做。我还问过其他似乎很广泛的问题,所以我把我使用的每段代码都与视图切换相关,减去了 viewDidLoad 等基本内容。

请阅读以下内容并按您认为合适的方式回答/评论。 谢谢你的帮助。

-(IBAction)startgame:(id)sender // 加载游戏画面

if(self.gamescreenViewController == nil)

    GameScreen *gameController = [[GameScreen alloc] initWithNibName:@"GameScreen" bundle:nil];
    self.gamescreenViewController = gameController;

    [gameController release];


// code to animate the switch

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

// load Ultimate tic tac toe view

if(self.ultimateViewController.view.superview == nil)

    // second code to animate switch

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];

    // [UIView setAnimationTransition: UIViewAnimationOptionTransitionCurlDown forView:self.view cache:YES];


    // show the ultimate view controller

    [ultimateViewController viewWillAppear:YES];
    [gamescreenViewController viewWillDisappear:YES];
    [gamescreenViewController.view removeFromSuperview];
    [self.view insertSubview:ultimateViewController.view atIndex:0];
    [gamescreenViewController viewDidDisappear:YES];
    [ultimateViewController viewDidAppear:YES];



    // change button image

    UIImage *buttonImage = [UIImage imageNamed:@"StartGame.png"];
    [switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [self.view addSubview:switchbutton];

    // setting values to zero after switch buttton action

    int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
    int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
    int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
    int turns = [[self.gamescreenViewController.turns text] intValue];


    symb1 = 0;
    symb2 = 0;
    symb3 = 0;
    turns = 1;

    [ultimateViewController.Symbolfield1 setText:[NSString stringWithFormat:@"%1i", symb1]];
    [ultimateViewController.Symbolfield2 setText:[NSString stringWithFormat:@"%1i", symb2]];
    [ultimateViewController.Symbolfield3 setText:[NSString stringWithFormat:@"%1i", symb3]];
    [gamescreenViewController.turns setText:[NSString stringWithFormat:@"%1i", turns]];

    // endable buttons after switch button

    ultimateViewController.a1.enabled = YES;
    ultimateViewController.a2.enabled = YES;
    ultimateViewController.a3.enabled = YES;
    ultimateViewController.a4.enabled = YES;
    ultimateViewController.a5.enabled = YES;
    ultimateViewController.a6.enabled = YES;
    ultimateViewController.a7.enabled = YES;
    ultimateViewController.a8.enabled = YES;
    ultimateViewController.a9.enabled = YES;
    ultimateViewController.a10.enabled = YES;
    ultimateViewController.a11.enabled = YES;
    ultimateViewController.a12.enabled = YES;
    ultimateViewController.b1.enabled = YES;
    ultimateViewController.b2.enabled = YES;
    ultimateViewController.b3.enabled = YES;
    ultimateViewController.b4.enabled = YES;
    ultimateViewController.b5.enabled = YES;
    ultimateViewController.b6.enabled = YES;
    ultimateViewController.b7.enabled = YES;
    ultimateViewController.b8.enabled = YES;
    ultimateViewController.b9.enabled = YES;
    ultimateViewController.b10.enabled = YES;
    ultimateViewController.b11.enabled = YES;
    ultimateViewController.b12.enabled = YES;
    ultimateViewController.c1.enabled = YES;
    ultimateViewController.c2.enabled = YES;
    ultimateViewController.c3.enabled = YES;
    ultimateViewController.c4.enabled = YES;
    ultimateViewController.c5.enabled = YES;
    ultimateViewController.c6.enabled = YES;
    ultimateViewController.c7.enabled = YES;
    ultimateViewController.c8.enabled = YES;
    ultimateViewController.c9.enabled = YES;
    ultimateViewController.c10.enabled = YES;
    ultimateViewController.c11.enabled = YES;
    ultimateViewController.c12.enabled = YES;

    gamescreenViewController.move1.enabled = YES;
    gamescreenViewController.move2.enabled = YES;
    gamescreenViewController.move3.enabled = YES;
    gamescreenViewController.move4.enabled = YES;
    gamescreenViewController.move5.enabled = YES;
    gamescreenViewController.move6.enabled = YES;
    gamescreenViewController.move7.enabled = YES;
    gamescreenViewController.move8.enabled = YES;
    gamescreenViewController.move9.enabled = YES;
    gamescreenViewController.move10.enabled = YES;
    gamescreenViewController.move11.enabled = YES;
    gamescreenViewController.move12.enabled = YES;
    gamescreenViewController.move13.enabled = YES;
    gamescreenViewController.move14.enabled = YES;
    gamescreenViewController.move15.enabled = YES;
    gamescreenViewController.move16.enabled = YES;
    gamescreenViewController.move17.enabled = YES;
    gamescreenViewController.move18.enabled = YES;
    gamescreenViewController.move19.enabled = YES;
    gamescreenViewController.move20.enabled = YES;
    gamescreenViewController.move21.enabled = YES;
    gamescreenViewController.move22.enabled = YES;
    gamescreenViewController.move23.enabled = YES;
    gamescreenViewController.move24.enabled = YES;
    gamescreenViewController.move25.enabled = YES;
    gamescreenViewController.move26.enabled = YES;
    gamescreenViewController.move27.enabled = YES;
    gamescreenViewController.move28.enabled = YES;
    gamescreenViewController.move29.enabled = YES;
    gamescreenViewController.move30.enabled = YES;
    gamescreenViewController.move31.enabled = YES;
    gamescreenViewController.move32.enabled = YES;
    gamescreenViewController.move33.enabled = YES;
    gamescreenViewController.move34.enabled = YES;
    gamescreenViewController.move35.enabled = YES;
    gamescreenViewController.move36.enabled = YES;
    gamescreenViewController.move37.enabled = YES;
    gamescreenViewController.move38.enabled = YES;
    gamescreenViewController.move39.enabled = YES;
    gamescreenViewController.move40.enabled = YES;
    gamescreenViewController.move41.enabled = YES;
    gamescreenViewController.move42.enabled = YES;
    gamescreenViewController.move43.enabled = YES;
    gamescreenViewController.move44.enabled = YES;
    gamescreenViewController.move45.enabled = YES;
    gamescreenViewController.move46.enabled = YES;
    gamescreenViewController.move47.enabled = YES;
    gamescreenViewController.move48.enabled = YES;
    gamescreenViewController.move49.enabled = YES;
    gamescreenViewController.move50.enabled = YES;
    gamescreenViewController.move51.enabled = YES;
    gamescreenViewController.move52.enabled = YES;
    gamescreenViewController.move53.enabled = YES;
    gamescreenViewController.move54.enabled = YES;
    gamescreenViewController.move55.enabled = YES;
    gamescreenViewController.move56.enabled = YES;
    gamescreenViewController.move57.enabled = YES;
    gamescreenViewController.move58.enabled = YES;
    gamescreenViewController.move59.enabled = YES;
    gamescreenViewController.move60.enabled = YES;
    gamescreenViewController.move61.enabled = YES;
    gamescreenViewController.move62.enabled = YES;
    gamescreenViewController.move63.enabled = YES;
    gamescreenViewController.move64.enabled = YES;
    gamescreenViewController.move65.enabled = YES;
    gamescreenViewController.move66.enabled = YES;
    gamescreenViewController.move67.enabled = YES;
    gamescreenViewController.move68.enabled = YES;
    gamescreenViewController.move69.enabled = YES;
    gamescreenViewController.move70.enabled = YES;
    gamescreenViewController.move71.enabled = YES;
    gamescreenViewController.move72.enabled = YES;
    gamescreenViewController.move73.enabled = YES;
    gamescreenViewController.move74.enabled = YES;
    gamescreenViewController.move75.enabled = YES;
    gamescreenViewController.move76.enabled = YES;
    gamescreenViewController.move77.enabled = YES;
    gamescreenViewController.move78.enabled = YES;
    gamescreenViewController.move79.enabled = YES;
    gamescreenViewController.move80.enabled = YES;
    gamescreenViewController.move81.enabled = YES;

    // clear images after switch button action

    gamescreenViewController.image1a.image = NO;
    gamescreenViewController.image2a.image = NO;
    gamescreenViewController.image3a.image = NO;
    gamescreenViewController.image4a.image = NO;
    gamescreenViewController.image5a.image = NO;
    gamescreenViewController.image6a.image = NO;
    gamescreenViewController.image7a.image = NO;
    gamescreenViewController.image8a.image = NO;
    gamescreenViewController.image9a.image = NO;
    gamescreenViewController.image1b.image = NO;
    gamescreenViewController.image2b.image = NO;
    gamescreenViewController.image3b.image = NO;
    gamescreenViewController.image4b.image = NO;
    gamescreenViewController.image5b.image = NO;
    gamescreenViewController.image6b.image = NO;
    gamescreenViewController.image7b.image = NO;
    gamescreenViewController.image8b.image = NO;
    gamescreenViewController.image9b.image = NO;
    gamescreenViewController.image1c.image = NO;
    gamescreenViewController.image2c.image = NO;
    gamescreenViewController.image3c.image = NO;
    gamescreenViewController.image4c.image = NO;
    gamescreenViewController.image5c.image = NO;
    gamescreenViewController.image6c.image = NO;
    gamescreenViewController.image7c.image = NO;
    gamescreenViewController.image8c.image = NO;
    gamescreenViewController.image9c.image = NO;
    gamescreenViewController.image1d.image = NO;
    gamescreenViewController.image2d.image = NO;
    gamescreenViewController.image3d.image = NO;
    gamescreenViewController.image4d.image = NO;
    gamescreenViewController.image5d.image = NO;
    gamescreenViewController.image6d.image = NO;
    gamescreenViewController.image7d.image = NO;
    gamescreenViewController.image8d.image = NO;
    gamescreenViewController.image9d.image = NO;
    gamescreenViewController.image1e.image = NO;
    gamescreenViewController.image2e.image = NO;
    gamescreenViewController.image3e.image = NO;
    gamescreenViewController.image4e.image = NO;
    gamescreenViewController.image5e.image = NO;
    gamescreenViewController.image6e.image = NO;
    gamescreenViewController.image7e.image = NO;
    gamescreenViewController.image8e.image = NO;
    gamescreenViewController.image9e.image = NO;
    gamescreenViewController.image1f.image = NO;
    gamescreenViewController.image2f.image = NO;
    gamescreenViewController.image3f.image = NO;
    gamescreenViewController.image4f.image = NO;
    gamescreenViewController.image5f.image = NO;
    gamescreenViewController.image6f.image = NO;
    gamescreenViewController.image7f.image = NO;
    gamescreenViewController.image8f.image = NO;
    gamescreenViewController.image9f.image = NO;
    gamescreenViewController.image1g.image = NO;
    gamescreenViewController.image2g.image = NO;
    gamescreenViewController.image3g.image = NO;
    gamescreenViewController.image4g.image = NO;
    gamescreenViewController.image5g.image = NO;
    gamescreenViewController.image6g.image = NO;
    gamescreenViewController.image7g.image = NO;
    gamescreenViewController.image8g.image = NO;
    gamescreenViewController.image9g.image = NO;
    gamescreenViewController.image1h.image = NO;
    gamescreenViewController.image2h.image = NO;
    gamescreenViewController.image3h.image = NO;
    gamescreenViewController.image4h.image = NO;
    gamescreenViewController.image5h.image = NO;
    gamescreenViewController.image6h.image = NO;
    gamescreenViewController.image7h.image = NO;
    gamescreenViewController.image8h.image = NO;
    gamescreenViewController.image9h.image = NO;
    gamescreenViewController.image1i.image = NO;
    gamescreenViewController.image2i.image = NO;
    gamescreenViewController.image3i.image = NO;
    gamescreenViewController.image4i.image = NO;
    gamescreenViewController.image5i.image = NO;
    gamescreenViewController.image6i.image = NO;
    gamescreenViewController.image7i.image = NO;
    gamescreenViewController.image8i.image = NO;
    gamescreenViewController.image9i.image = NO;
    gamescreenViewController.playerturnimage.image = NO;

    gamescreenViewController.playerturnlabel.text = @"Your Turn Player One";




else

    // load game screen view
    if(self.gamescreenViewController.view.superview == nil)
    

        int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
        int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
        int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];

        if ((symb1 < 1) || (symb2 <1) || (symb3 <1))
        
            // warning message

            UIAlertView *alertview1 = [[ UIAlertView alloc]
                                       initWithTitle:@"No Symbols Selected"
                                       message:@"\n You must select an \n image for each player \n\n"
                                       delegate:nil cancelButtonTitle:@"Got it"
                                       otherButtonTitles: nil];

            [alertview1 show];
            [alertview1 release];

        

        else
        
            // show the game screen

            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

            // show the game controller

            [gamescreenViewController viewWillAppear:YES];
            [ultimateViewController viewWillDisappear:YES];
            [ultimateViewController.view removeFromSuperview];
            [self.view insertSubview:gamescreenViewController.view atIndex:0];
            [ultimateViewController viewDidDisappear:YES];
            [gamescreenViewController viewDidAppear:YES];


            // change button image

            UIImage *buttonImage = [UIImage imageNamed:@"EndGame.png"];
            [switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
            [self.view addSubview:switchbutton];

            // hides and disables the undo button for the first move

            gamescreenViewController.undobutton.enabled = NO;
            gamescreenViewController.undobutton.hidden = YES;


        

        // send values from ultimate viewcontroller to gamescreen

        int playerpick = [[self.ultimateViewController.SegmentChange text] intValue];
        [self.gamescreenViewController.SegmentChange2a setText:[NSString stringWithFormat:@"%1i",playerpick]];

        int symbol1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
        [self.gamescreenViewController.Symbolfield1a setText:[NSString stringWithFormat:@"%1i",symbol1]];

        int symbol2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
        [self.gamescreenViewController.Symbolfield2a setText:[NSString stringWithFormat:@"%1i",symbol2]];

        int symbol3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
        [self.gamescreenViewController.Symbolfield3a setText:[NSString stringWithFormat:@"%1i",symbol3]];
    


// statement to enact switch animation

[UIView commitAnimations];

请记住,这设置为我正在制作的某个游戏,作为连接到开始按钮的动作。

有什么建议吗?

【问题讨论】:

除了“代码”之外,您没有告诉我们什么不起作用 代码无法识别 viewWillAppear 和 viewWillDisappear 的地方。 如果我站在你的立场上,我会停下来阅读一些文档。这是视图控制器指南:developer.apple.com/library/ios/featuredarticles/…,然后是 UIViewController 的文档:developer.apple.com/library/ios/documentation/uikit/reference/… 谢谢。我试图在我的 IOS6 开发书中找到它,但找不到。 【参考方案1】:

看起来您实际上并未呈现从笔尖加载的视图。

【讨论】:

以上是关于我正在 IOS 7 中为游戏切换视图,但无法使此代码正常工作。有啥建议么?的主要内容,如果未能解决你的问题,请参考以下文章

约束常量在 iOS 7 中为零,但在 iOS 8 中没有

在 Mapbox 中,如何从 iOS 上的样式切换图层?

无法以编程方式在视图 Drupal 7 中为日期设置公开过滤器

在iOS 5中使用UISegmentedControl切换视图控制器

iOS 7.1 - 7.1.2 用户无法打开我的游戏

删除 iOS 7 状态栏 [关闭]