ios - 无法弄清楚 UILabel 不显示分配给它的字符串的原因 - Quote Quiz App

标签 ios objective-c

我一直在 raywenderlich.com 上关注在线教程,遇到了一个小障碍。在教程 ( Quote Quiz ) 中,我似乎做错了什么,因为从 Quote.plist 中提取的字符串根本没有显示在问题和答案标签上。

这是我的 View Controller .h:

 #import <UIKit/UIKit.h>
@class Quiz;
@interface ViewController : UIViewController
@property (nonatomic, assign) NSInteger quizIndex;
@property (nonatomic, strong) Quiz* quiz;
@property (nonatomic, weak) IBOutlet UILabel* questionLabel;
@property (nonatomic, weak) IBOutlet UILabel* answer1Label;
@property (nonatomic, weak) IBOutlet UILabel* answer2Label;
@property (nonatomic, weak) IBOutlet UILabel* answer3Label;
@property (nonatomic, weak) IBOutlet UIButton* answer1Button;
@property (nonatomic, weak) IBOutlet UIButton* answer2Button;
@property (nonatomic, weak) IBOutlet UIButton* answer3Button;
@property (nonatomic, weak) IBOutlet UIImageView* movie1;
@property (nonatomic, weak) IBOutlet UIImageView* movie2;
@property (nonatomic, weak) IBOutlet UIImageView* movie3;
@property (nonatomic, weak) IBOutlet UILabel* statusLabel;
@property (nonatomic, weak) IBOutlet UIButton* startButton;
@property (nonatomic, weak) IBOutlet UIButton* infoButton;
@end

.m

#import "ViewController.h"
#import "Quiz.h"

@interface ViewController ()
@property (nonatomic, assign) NSInteger answer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.quizIndex = 999;
    self.quiz = [[Quiz alloc] initWithQuiz:@"Quotes"];
    self.questionLabel.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
    [self nextQuizItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) nextQuizItem {
    if(self.quizIndex == 999){
        self.quizIndex = 0;
        self.statusLabel.text = @"";
    }else if((self.quiz.quizCount-1) > self.quizIndex) {
        self.quizIndex++;
    }else{
        self.quizIndex = 0;
        self.statusLabel.text = @"";
    }

    if(self.quiz.quizCount >= (self.quizIndex+1)){
        self.questionLabel.text = self.quiz.quote;
        self.answer1Label.text = self.quiz.ans1;
        self.answer2Label.text = self.quiz.ans2;
        self.answer3Label.text = self.quiz.ans3;
    }else{
        self.quizIndex = 0;
        [self quizDone];
    }

    self.answer1Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
    self.answer2Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
    self.answer3Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];

    self.answer1Button.hidden = NO;
    self.answer2Button.hidden = NO;
    self.answer3Button.hidden = NO;
}

-(void) quizDone {

}
@end

还有我的 Quiz.h:

#import <Foundation/Foundation.h>

@interface Quiz : NSObject
@property (nonatomic, strong)  NSMutableArray* movieArray;
@property (nonatomic, assign) NSInteger correctCount;
@property (nonatomic, assign) NSInteger incorrectCount;
@property (nonatomic, assign) NSInteger quizCount;
@property (nonatomic, readonly, strong) NSString* quote;
@property (nonatomic, readonly, strong) NSString* ans1;
@property (nonatomic, readonly, strong) NSString* ans2;
@property (nonatomic, readonly, strong) NSString* ans3;

-(id)initWithQuiz:(NSString*)plistName;
-(void)nextQuestion:(NSUInteger)idx;
-(BOOL)checkQuestion:(NSUInteger)question forAnswer:(NSUInteger)answer;
@end

和 Quiz.m:

#import "Quiz.h"

@interface Quiz()
@property (nonatomic, strong) NSString* quote;
@property (nonatomic, strong) NSString* ans1;
@property (nonatomic, strong) NSString* ans2;
@property (nonatomic, strong) NSString* ans3;
@end

@implementation Quiz

-(id)initWithQuiz:(NSString *)plistName {
    if((self=[super init])) {
        NSString* plistCatPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
        self.movieArray = [NSMutableArray arrayWithContentsOfFile:plistCatPath];
        self.quizCount = [self.movieArray count];
    }
    return self;
}

-(void)nextQuestion:(NSUInteger)idx {
    self.quote = [NSString stringWithFormat:@"%@", self.movieArray[idx][@"quote"]];

    self.ans1 = self.movieArray[idx][@"ans1"];
    self.ans2 = self.movieArray[idx][@"ans2"];
    self.ans3 = self.movieArray[idx][@"ans3"];

    if(idx == 0){
        self.correctCount = 0;
        self.incorrectCount = 0;
    }
}

-(BOOL)checkQuestion:(NSUInteger)question forAnswer:(NSUInteger)answer {
    NSString* ans = [NSString stringWithFormat:@"%@", self.movieArray[question][@"answer"]];

    if([ans intValue] == answer) {
        self.correctCount++;
        return YES;
    }else{
        self.incorrectCount++;
        return NO;
    }
}
@end

我使用的是最新的 Xcode 6, socket 已连接,我使用的 plist 文件名为“Quotes.plist”。

如有任何帮助或建议,我们将不胜感激。

谢谢。

最佳答案

在显示它之前,您不会得到下一个测验问题。

在 ViewController.m 中 nextQuizItem 的第二个 if 语句中添加以下内容:

[self.quiz nextQuestion:self.quizIndex];

所以看起来像这样

if(self.quiz.quizCount >= (self.quizIndex+1)){

    [self.quiz nextQuestion:self.quizIndex]; // <-- ADD THIS

    self.questionLabel.text = self.quiz.quote;
    self.answer1Label.text = self.quiz.ans1;
    self.answer2Label.text = self.quiz.ans2;
    self.answer3Label.text = self.quiz.ans3;
}else{
    self.quizIndex = 0;
    [self quizDone];
}

关于ios - 无法弄清楚 UILabel 不显示分配给它的字符串的原因 - Quote Quiz App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27428586/

相关文章:

ios - 来自 AVMetadataItem objective-c 的 ISRC 代码

iOS:是另一个方向的 prepareForSegue 回调(即按下后退按钮)

android - 如何将flutter Agora RTC Engine视频聊天保存到本地存储?

ios - 使用 `Visual Effect View with Blur` 减少模糊?

objective-c - 如何从 Objective-C 调用带有闭包的 Swift 异步方法

iphone - 方法dismissviewdidfinish的重复声明

ios - 将 UIImage 设置为 UIImageView 以节省内存时是否需要调整 UIImage 的大小?

ios - ScrollView 中的内容随着导航菜单的高度值向下移动

iphone - iOS:如何创建和绘制(并保存)比屏幕大的图像?

objective-c - Objective-C : Missing architecture