有什么想法吗?
应用程序成功运行,但单击按钮不会显示任何问题。这是BNR书(第4版)中代码的精确副本。我认为本书中的Xcode版本是6.并不认为这会有很大的不同。
运行Xcode 6.1
//
// ViewController.m
// Quiz
//
// Created by S on
// Copyright (c) 2014 S. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) int currentQuestionIndex;
@property (nonatomic, copy) NSArray *questions;
@property (nonatomic, copy) NSArray *answers;
@property (nonatomic, weak) IBOutlet UILabel *questionLabel;
@property (nonatomic, weak) IBOutlet UILabel *answerLabel;
@end
@implementation ViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
// Call the init method implemented by the superclass
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create two arrays filled with questions and answers
// and make the pointers point to them
self.questions = @[@"From what is cognac made?",
@"What is 7+7?",
@"What is the capital of Vermont?"];
self.answers = @[@"Grapes",
@"14",
@"Montpelier"];
}
// Return the address of the new object
return self;
}
- (IBAction)showQuestion:(id)sender
{
// Step to the next question
self.currentQuestionIndex++;
// Am I past the last question?
if (self.currentQuestionIndex == [self.questions count]) {
// Go back to the first question
self.currentQuestionIndex = 0;
}
// Get the string at that index in the questions array
NSString *question = self.questions[self.currentQuestionIndex];
// Display the string in the question label
self.questionLabel.text = question;
// Reset the answer label
self.answerLabel.text = @"???";
}
- (IBAction)showAnswer:(id)sender
{
// What is the answer to the current question?
NSString *answer = self.answers[self.currentQuestionIndex];
// Display it in the answer label
self.answerLabel.text = answer;
}
@end