gpt4 book ai didi

ios - show Question 显示问题(null)而不是存储在数组中的问题

转载 作者:行者123 更新时间:2023-12-01 19:07:04 25 4
gpt4 key购买 nike

我目前正在使用 xcode5。我基本上打算创建一个测验应用程序。当我单击显示问题按钮时,它应该给我存储在数组中的下一个问题。但是,它一直给我(null)。谁能帮我解决这个问题?提前致谢。

//
// Quiz2ViewController.h
// Quiz2
//
//
#import <UIKit/UIKit.h>
@interface Quiz2ViewController : UIViewController
{
int currentQuestionIndex;

NSMutableArray *questions;
NSMutableArray *answers;

IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
}
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
@end

.m 文件
//
// Quiz2ViewController.m
// Quiz2
//
//
#import "Quiz2ViewController.h"

@interface Quiz2ViewController ()

@end

@implementation Quiz2ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

-(id)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 and make the pointers point to them

questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];

//Add questions and answers to the arrays
[questions addObject:@"what is 7 + 7?"];
[answers addObject:@"14"];

[questions addObject:@"what is the capital of Vermont?"];
[answers addObject:@"Montpelier"];

[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];}

//return the address of the new object
return self;
}

- (IBAction)showQuestion:(id)sender
{
// Step to the next question
currentQuestionIndex++;
// Am i past the last Question?
if (currentQuestionIndex == [questions count]){

//Go back to the first question
currentQuestionIndex = 0;
}

// Get the string at that index in the quetions array
NSString *question = [questions objectAtIndex:currentQuestionIndex];

// Log the strin gto the console
NSLog(@"displaying question: %@",question);

//display the stirng in the question field
[questionField setText:question];

//clear the answer field
[answerField setText: @"???"];
}

-(IBAction)showAnswer:(id)sender
{
//What is the answer to the current question?
NSString *answer = [answers objectAtIndex:currentQuestionIndex];

//Display it in the answer field
[answerField setText:answer];
}

@end

最佳答案

我知道这已经是两个月大了,但对于那些阅读 Big Nerd Ranch iOS 编程书的人来说,这是一个常见的问题。永远不会调用 initNibWithName 方法,因为您没有使用 NIB 文件 - 您正在使用 Storyboard 。要使此代码正常工作,只需删除该方法

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

然后将代码放在 viewDidLoad block 中
- (void)viewDidLoad
{
[super viewDidLoad];
if (self){

//Create two arrays and make the pointers point to them

questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];

//Add questions and answers to the arrays
[questions addObject:@"what is 7 + 7?"];
[answers addObject:@"14"];

[questions addObject:@"what is the capital of Vermont?"];
[answers addObject:@"Montpelier"];

[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];}

}
}

关于ios - show Question 显示问题(null)而不是存储在数组中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995804/

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