クルットまわしてすうじを2つえらんでみよう。
すうじを足したこたえがマルの中にでてくるよ!
という感じの子供向けiPhoneゲームアプリのサンプルコードを書いてみた。

ポイント
2つのTableViewをViewの中に表示しています。
メインのViewControllerの中に、TableViewControllerの実装を
AdditionTVCという名前で作成して、それをTableViewのdelegateに入れました。
さらに、TableViewを選択したときのイベントを拾うために、
Messageという名前でProtocolを付けています。

環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています

iPhone 足し算アプリのサンプルコード

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@protocol Message <NSObject>

– (void)hello;

@end

@interface AdditionTVC : ViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSString *currentNumber;

@property (nonatomic, assign) id messageDelegate;

@end

@implementation AdditionTVC

@synthesize currentNumber;

@synthesize messageDelegate;

#pragma mark – Table view data source

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 100;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @”Cell”;

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.textLabel.text = [NSString stringWithFormat:@”%d”, indexPath.row];

    cell.textLabel.textAlignment = 1;

    return cell;

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 20;

}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    self.currentNumber = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;

    [messageDelegate hello];

}

@end

@interface ViewController () <Message>

@property (nonatomic, strong) AdditionTVC *rightSide;

@property (nonatomic, strong) AdditionTVC *leftSide;

@property (nonatomic, strong) UILabel *answerLabel;

@end

@implementation ViewController

@synthesize rightSide, leftSide, answerLabel;

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createSigns];

    [self createRightSide];

    [self createLeftSide];

    [self createAnswerLabel];

}

– (void)createRightSide

{

    rightSide = [[AdditionTVC alloc] init];

    rightSide.messageDelegate = self;

    

    UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(10, 100, 50, 100) style:UITableViewStylePlain];

    tv.delegate = rightSide;

    tv.dataSource = rightSide;

    [self.view addSubview:tv];

}

– (void)createLeftSide

{

    leftSide = [[AdditionTVC alloc] init];

    leftSide.messageDelegate = self;

    

    UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 50, 100) style:UITableViewStylePlain];

    tv.delegate = leftSide;

    tv.dataSource = leftSide;

    [self.view addSubview:tv];

}

– (void)createAnswerLabel

{

    answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 100, 100, 100)];

    answerLabel.layer.cornerRadius = 50;

    answerLabel.textAlignment = 1;

    answerLabel.font = [UIFont boldSystemFontOfSize:50];

    [self.view addSubview:answerLabel];

}

– (void)createSigns

{

    UIView *backPlate = [[UIView alloc] initWithFrame:CGRectMake(1, 70, 318, 150)];

    backPlate.backgroundColor = [UIColor darkGrayColor];

    backPlate.layer.cornerRadius = 20;

    [self.view addSubview:backPlate];

    

    

    UILabel *plusSign = [[UILabel alloc] init];

    plusSign.text = @”+”;

    plusSign.font = [UIFont boldSystemFontOfSize:40];

    plusSign.textColor = [UIColor whiteColor];

    [plusSign sizeToFit];

    plusSign.center = CGPointMake(80, 150);

    plusSign.backgroundColor = [UIColor clearColor];

    [self.view addSubview:plusSign];

    

    

    UILabel *equalSign = [[UILabel alloc] init];

    equalSign.text = @”=”;

    equalSign.font = [UIFont boldSystemFontOfSize:40];

    equalSign.textColor = [UIColor whiteColor];

    [equalSign sizeToFit];

    equalSign.center = CGPointMake(180, 150);

    equalSign.backgroundColor = [UIColor clearColor];

    [self.view addSubview:equalSign];

}

– (void)hello

{

    int number = [self.rightSide.currentNumber intValue];

    number += [self.leftSide.currentNumber intValue];

    self.answerLabel.text = [NSString stringWithFormat:@”%d”, number];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end