第一章Practice

 

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

 

第一章Practice

 

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

第一章Practice

 

3.输入项目名称

第一章Practice

 

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


如图:
第一章Practice

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

如图:
第一章Practice

 

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

第一章Practice

7.点击Run,进入模拟器



第一章Practice

8.点击Show question


第一章Practice

第一章Practice

 

9.点击Show answer

第一章Practice

 

10.点击Show question

第一章Practice

 

11.点击Show answer

第一章Practice

12.点击Show question


第一章Practice

13.点击Show answer


第一章Practice

在输出窗口中会看到图中内容:
第一章Practice


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值