@neiraza

2匹の怪獣が寝た後にプログラマーしてる、最近はサイバーエージェントでゼミ長もしてる僕のネタ帳

iPhoneアプリ開発講座を受講する その4

[iPhone][Objective-C]

いよいよ明日は第3回目の講座。
なので復習も今日で一段落ってことで。
・・・そろそろ、Jenkins Advent Calendarの準備しないといかんし・・・。

UINavigationController

画面繊維を管理するコントロールクラス。
ルート画面を設定して、その下にサブ画面がぶら下がる感じらしい。

今回はルート画面にFirstViewController、
ボタンイベントで遷移する先をSecondViewControllerで表現してみる。

ルート画面を定義してみた。

AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController * navi_;
    
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    .....

    FirstViewController * firstViewController = [[FirstViewController alloc]init];
    [firstViewController.view setFrame:CGRectMake(0, 0, 320, 480)];
    navi_ = [[UINavigationController alloc]initWithRootViewController:firstViewController];
    [firstViewController release];
    
    ......
}

ルート画面にはボタンを1つ置いてみた。
[次へ]ボタン押下で、次画面遷移を行う。

FirstViewController.m
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"最初の画面へ";
    UIButton * nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nextButton setFrame:CGRectMake(0, 0, 100, 50)];
    [nextButton setTitle:@"次へ" forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(doNext) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
                             
}

-(void)doNext
{
    NSLog(@"次へ");
    SecondViewController * secondViewController  = [[SecondViewController alloc]init];
    [secondViewController.view setFrame:CGRectMake(0, 0, 320, 480)];
    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];
}

遷移確認用の画面を用意。

SecondViewController.m
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    self.title = @"次の画面へ";
    
    [super viewDidLoad];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [label setText:@"こんにちわ"];
    [self.view addSubview:label];
}


OKOK.