クラスの継承2 | 成長の果実

成長の果実

不完全でも良いから前に進む。

[前回の記事]

クラスの継承
http://ameblo.jp/welx/entry-11011228858.html

---------------------------------------------


前回の記事とは別のクラス継承のお話。


今回やること。


1.オブジェクトを宣言するクラス(DBClass)を作成する。

2.アプリ起動時に表示される FirstViewController を作成する。
  DBClassで宣言したオブジェクトに適当な変数を格納する。

3,SecondViewControllerを作成する。
  FirstViewControllerで格納したDBClassのオブジェクトの内容を表示させる。




それではDBClassを作成していく。

主要部分だけを説明していく。赤字の部分がポイント箇所。


◎DBClass.h

#import <Foundation/Foundation.h>


@interface DBClass : NSObject {

}

@property(nonatomic,copy)NSString *str;
@property(nonatomic,assign)NSInteger num;


@end



◎DBClass.m

#import "DBClass.h"


@implementation DBClass

@synthesize str = _str;
@synthesize num = _num;


- (void)dealloc
{
[_str release];

[super dealloc];
}

@end



DBClassでは文字列を格納する str と、数値を格納する num のオブジェクトを宣言しておく。


次に FirstViewController を作成する。


◎FirstViewController.h

#import <Foundation/Foundation.h>
#import "SecondViewController.h"


@interface FirstViewController : UIViewController {

}

@end



後ほど作成するSecondViewControllerをFirstViewControllerのビューに乗せるため、importを記述している。


◎FirstViewController.m

#import "FirstViewController.h"


@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

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

// ナビゲーションバーを非表示にする
[self.navigationController setNavigationBarHidden:YES animated:NO];

// ビューの背景色
self.view.backgroundColor = [UIColor blueColor];

// ラベル
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 100, 40);
label.backgroundColor = [UIColor clearColor]; // 背景色
label.textColor = [UIColor blackColor]; // テキストの色
label.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; // フォント
label.text = @"FirstView"; // 表示内容
[self.view addSubview:label];
[label release];


// DBClassを初期化
DBClass *dbClass = [[DBClass alloc] init];

// 値設定
dbClass.str = @"あいうえお";
dbClass.num = 12345;

// SecondViewControllerを初期化
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc] initDBClassValue:dbClass];

// SecondViewController を表示
[self.view addSubview:secondViewController.view];

}

@end



DBClassで宣言したオブジェクトにそれぞれ値を格納し、SecondViewControllerをaddSubviewしている。


最後にSecondViewControllerを作成する。


◎SecondViewController.h

#import <Foundation/Foundation.h>
#import "DBClass.h"


@interface SecondViewController : UIViewController {

}

@property (nonatomic,retain) DBClass *dbClass;

// init
- (id)initDBClassValue:(DBClass *)db;


@end



DBClassをimportしている。


◎SecondViewController.m

#import "SecondViewController.h"


@implementation SecondViewController

//--------------------------------------------------
// getter, setterの設定
//--------------------------------------------------
@synthesize dbClass = _dbClass;


//--------------------------------------------------
// init
//--------------------------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

// DBClass
- (id)initDBClassValue:(DBClass *)db
{
self = [super init];
if (self) {
self.dbClass = db;
}
return self;
}


- (void)dealloc
{
[_dbClass release];

[super dealloc];
}

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

// フレームサイズ
self.view.frame = CGRectMake(50, 50, 220, 360);

// ビューの背景色
self.view.backgroundColor = [UIColor yellowColor];

// ラベル
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 100, 40);
label.backgroundColor = [UIColor clearColor]; // 背景色
label.textColor = [UIColor blackColor]; // テキストの色
label.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; // フォント
label.text = @"SecondView"; // 表示内容
[self.view addSubview:label];
[label release];


// ラベル - DBClass: str
UILabel *labelStr = [[UILabel alloc] init];
labelStr.frame = CGRectMake(10, 100, 200, 40);
labelStr.backgroundColor = [UIColor darkGrayColor]; // 背景色
labelStr.textColor = [UIColor whiteColor]; // テキストの色
labelStr.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; // フォント
labelStr.textAlignment = UITextAlignmentCenter; // 中央寄せ
labelStr.text = self.dbClass.str; // 表示内容
[self.view addSubview:labelStr];
[labelStr release];

// ラベル - DBClass: num
UILabel *labelNum = [[UILabel alloc] init];
labelNum.frame = CGRectMake(10, 230, 200, 40);
labelNum.backgroundColor = [UIColor darkGrayColor]; // 背景色
labelNum.textColor = [UIColor whiteColor]; // テキストの色
labelNum.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; // フォント
labelNum.textAlignment = UITextAlignmentCenter; // 中央寄せ
labelNum.text = [[NSString alloc] initWithFormat:@"%d", self.dbClass.num]; // 表示内容
[self.view addSubview:labelNum];
[labelNum release];
}

@end



DBClassを初期化して、FirstViewControllerで格納された値をラベルで表示させている。



ビルドして実行すると以下のように結果が表示される。


$成長の果実-SampleSuccession2


少々分かりづらいが、そのうち慣れていくはず。


----------
サンプルソース:https://github.com/tetsuco/SampleSuccession2