在不同的视图控制器中显示用户操作的结果

时间:2014-01-16 12:23:56

标签: ios objective-c uiimageview

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

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

  1. 有没有办法从其他控制器显示结果(错误/右图)?

  2. 或者是否有我可以修复的代码,例如,如果结果图像只显示几秒钟,然后自动继续游戏?

  3. 这是我目前的代码:

    #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;        
    

1 个答案:

答案 0 :(得分:3)

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

您应该在ResultViewController中编写一些代码,以便在例如点按图像本身或按钮时自行解除。

我认为“Game”是你的主视图控制器,即使你的代码不清楚它是什么:)

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

在Game.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

以上代码在显示后5秒钟弹出视图控制器。

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

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