1.打开Xcode,选择Creat a new Xcode project

2.选择Application中的Single View Application,点击Next

3.输入项目名称

4.在ViewCotroller.h中输入如下代码
#import
@interface ViewController : UIViewController{
int currentQuestionIndex;
NSMutableArray* question;
NSMutableArray* answer;
IBOutlet UILabel* questionFiled;
IBOutlet UILabel* answerFiled;
}
-(IBAction)showQuestion:(id)sender;
-(IBAction)showAnswer:(id)sender;
@end
如图:

5.在ViewCrotroller.m中输入如下代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
question=[[NSMutableArray alloc]init];
answer=[[NSMutableArray alloc]init];
[question addObject:@"What is 7+7?"];
[answer addObject:@"14"];
[question addObject:@"What is your name?"];
[answer addObject:@"zzg"];
[question addObject:@"What is your sex?"];
[answer addObject:@"male"];
}
return self;
}
-(IBAction)showQuestion:(id)sender{
currentQuestionIndex++;
if(currentQuestionIndex==[question count]){
currentQuestionIndex=0;
}
NSString *questions=[question objectAtIndex:currentQuestionIndex];
NSLog(@"display question:%@",questions);
[questionFiled setText:questions];
[answerFiled setText:@" "];
}
-(IBAction)showAnswer:(id)sender{
NSString *answers=[answer objectAtIndex:currentQuestionIndex];
[answerFiled setText:answers];
}
@end
如图:

6.点击ViewController.xib,建立视图,右击File Owner,建立联系

7.点击Run,进入模拟器

8.点击Show question


9.点击Show answer

10.点击Show question

11.点击Show answer

12.点击Show question

13.点击Show answer

在输出窗口中会看到图中内容:
