gpt4 book ai didi

ios - 在不同的 View Controller 中显示用户操作的结果

转载 作者:行者123 更新时间:2023-12-01 19:03:42 26 4
gpt4 key购买 nike

我是应用程序开发的新手。我正在创建一个测验应用程序。如何将正确/错误答案的结果图像显示到另一个页面(ViewController)?如果有人可以帮助我,我将非常感激。

目前它在同一个 Controller 中显示图像,但对我来说问题是当结果图像在正确或错误答案后显示时......它不会返回继续游戏......

  • 有没有办法从其他 Controller 显示结果(错误/正确)图像?
  • 或者有什么我可以修复的代码,比如如果结果图像只显示几秒钟然后自动继续游戏?

  • 这是我当前的代码:
    #import "Game.h"

    @interface Game ()
    @property (strong, nonatomic) IBOutlet UIImageView *ImageView;
    @property (strong, nonatomic) IBOutlet UIImageView *Randonimage;

    @end

    @implementation Game

    -(void)RightAnswer{

    ScoreNumber = ScoreNumber + 1;
    Score.text = [NSString stringWithFormat:@"%i", ScoreNumber];
    Answer1.hidden = YES;
    Answer2.hidden = YES;
    Answer3.hidden = YES;
    QuestionText.hidden = YES;
    CategorySelected.hidden = YES;
    Results.hidden = NO;
    Results.image = [UIImage imageNamed:@"right.png"];
    Randomimage.hidden = YES;
    }

    -(void)WrongAnswer{

    LivesNumber = LivesNumber - 1;
    Lives.text = [NSString stringWithFormat:@"%i", LivesNumber];
    Answer1.hidden = YES;
    Answer2.hidden = YES;
    Answer3.hidden = YES;
    QuestionText.hidden = YES;
    CategorySelected.hidden = YES;
    Results.hidden = NO;
    Results.image = [UIImage imageNamed:@"wrong.png"];
    Randomimage.hidden = YES;

    if(LivesNumber ==0) {
    Results.image = [UIImage imageNamed:@"gameover.png"];
    GameInProgress = NO;

    最佳答案

    根据您的应用程序的结构,您应该创建一个新的 View Controller (例如 ResultViewController )并将其推送到您的导航 Controller 或将其显示为模态视图或模态导航 Controller 。

    你应该在你的 ResultViewController 中写一些代码。例如,当图像本身或按钮被点击时,会自行关闭。

    我假设“游戏”是您的主视图 Controller ,即使您的代码不清楚它是什么:)

    下面是基于 ARC 的代码(RightAnswer 用于显示模式,但它也适用于 WrongAnswer):

    在游戏.m

    - (void)RightAnswer {

    ScoreNumber = ScoreNumber + 1;
    Score.text = [NSString stringWithFormat:@"%i", ScoreNumber];
    Answer1.hidden = YES;
    Answer2.hidden = YES;
    Answer3.hidden = YES;
    QuestionText.hidden = YES;
    CategorySelected.hidden = YES;

    ResultViewController *vc = [[ResultViewController alloc] initWithNibName:nil bundle:nil image:[UIImage imageNamed:@"right.png"]];
    [self.navigationController pushViewController:vc animated:YES];
    }

    在 ResultViewController.h
    @interface ResultViewController : UIViewController

    @property (nonatomic, strong) IBOutlet UIImageView *imageView;

    @end

    在 ResultViewController.m 中(假设您的 imageView 属性在 Interface Builder 中正确链接)
    #import "ResultViewController.h"

    @implementation ResultViewController {

    UIImage *_image;
    }

    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil image:(UIImage *)image {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    _image = image;
    }
    return self;
    }

    - (void)viewDidLoad {

    [super viewDidLoad];
    self.imageView.image = _image;
    }

    - (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dismiss) userInfo:nil repeats:NO];
    }

    - (void)dismiss {

    [self.navigationController popViewControllerAnimated:YES];
    }

    @end

    上面的代码在 View Controller 显示 5 秒后弹出 View Controller 。

    不要忘记 #import "ResultViewController.h"Game.m .

    当然这些代码只是一个 stub ,可以改进很多,如果您需要更具体的建议,请发表评论。

    关于ios - 在不同的 View Controller 中显示用户操作的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21161685/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com