2012年3月13日火曜日

Modal Viewの表示


UIViewContollerのサブクラス (MyViewController) を追加する。

Storyboardで部品を追加し、プロパティーを設定する。
・UIViewContollerを追加し、クラスをMyViewControllerに変更する。
・EmbededIn... >Navigation ControllerでUINavaigationContollerを追加する。
・追加したUINavaigationContollerにidenfifier("ModalViewNavCtl")を設定する。

//モーダルView Controllerを開くView Controllerのメソッド
- (IBAction)showModalView:(id)sender {
    //ModalViewNavCtl: Storyboadで設定したUINavigationController のidentifier
    UINavigationController *navCtl = [[self storyboard]
                                      instantiateViewControllerWithIdentifier:@"ModalViewNavCtl"];
    //UINavigationControllerのdelegateにする場合。
    navCtl.delegate = self;
//モーダルView Controllerを表示する。
    [self presentModalViewController:navCtl animated:YES];
}

//モーダルView Controller表示前に呼ばれる。
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    viewContrller.delegate = self;
}

//モーダルView Controller表示後に呼ばれる。
- (void)navigationController:(UINavigationController *)navigationController
    didShowViewController:(UIViewController *)viewController
            animated:(BOOL)animated
{
}

iOS View Controllerプログラミングガイド」(116ページ)ではモーダルView Controllerを開いたView Controllerで閉じる方法を推奨しています。

//モーダルView Controller(MyViewController)のメソッド
- (IBAction)closeMoadlView:(id)sender
{
    //delegate=モーダルView Controllerを開いたView Controller
    //someMethod
    [delegate someMethod;self];
    //自分で閉じることもできますが、推奨されていません。
    //[self dismissModalViewControllerAnimated:YES];
}

//モーダルView Controllerを開いたView Controllerのメソッド
- (void)someMethod:(UIViewController *)modalView
{
    //モーダルView Controllerを閉じる
    [modalView dismissModalViewControllerAnimated:YES];
}

0 件のコメント: