ModalViewのお話(3) | 初心者がiPhoneアプリを作るブログ

初心者がiPhoneアプリを作るブログ

初心者がiPhoneアプリを作るブログです.
入門レベルですので開発初心者にも,できるだけ分かるように丁寧に説明していきます(多分).

さて,ModalViewControllerについて見ます.
このクラスはUIViewControllerを継承して作られています.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:1.0 green:0.5 blue:0.0 alpha:0.1]];


    
CGFloat frameWidth = self.view.frame.size.width;
    CGFloat frameHeight = self.view.frame.size.height;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(frameWidth / 2 - 40, frameHeight / 2 - 20, 80, 40)];
    [button setTitle:@"Back" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:button];
}

- (void)buttonTouched:(UIButton *)button{
    [self dismissModalViewControllerAnimated:YES];
}


「.h」ファイルには何も書いていません.
「.m」ファイルのviewDidLoadと,その下に新しく関数を追加しました.

もうほとんど解説することはありません.
モーダルビューとして表示するビューに「戻るボタン」を配置しています.
ボタンをタッチすると以下のコードが呼ばれ,元の画面に戻ります.
[self dismissModalViewControllerAnimated:YES];

もう簡単ですね.

さて,ここから少しおまけなのですが,モーダルビューを呼び出す際のアニメーションは
4パターンあります.
設定の仕方は,モーダルビューとして呼び出すコントローラー
["インスタンス名" setModalTransitionStyle:"パラメータ"];

を呼び出すだけです.

例えば,ModalViewControllerの初期化メソッドの中(initWithNibName中)で,
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

を呼びます.パラメータは4種類あり,
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleCrossDissolve
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStylePartialCurl

が使えます.
色々試してみて下さい.

次の記事へ

HOMEに戻る