47
47

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

StoryBoardを利用した制御関連コード

Posted at

遷移開始

[self performSegueWithIdentifier:@"openCameraFinder" sender:self];

モーダルビューから、親ビューへ戻る

[self dismissViewControllerAnimated:YES completion:nil];

StoryBoard 遷移前に遷移先のコントローラに値を渡すとき

prepareForSegue:senderは、api裏側でControllerオブジェクトのメモリが確保された後、viewDidLoadが呼ばれる前に呼び出される。

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {

    // segue のidentifier で、どの画面遷移か判別する
    if( [segue.identifier isEqualToString:@"showNextView"] ) {
        // 遷移先のUIViewControllerを指定
        NextViewController* nvc = (NextViewController*)[segue destinationViewController];
        nvc.delegate =self;
         
        /* ここで遷移先画面にデータを渡したりする */
    }

}

モーダルビューから、遷移元に値を戻す

デリゲートを使い、親で定義されたメソッドを呼び出す手順になる。

ChildSampleViewController.h
# import <UIKit/UIKit.h>

@protocol ChildSampleViewControllerDelegate;

@interface ChildSampleViewController : UIViewController

// CameraViewControllerへ戻るためのデリゲート(デリゲートオブジェクトは基本親持ち)
@property (weak , nonatomic) id <ChildSampleViewControllerDelegate> delegate;

- (IBAction)pushCloseButton:(id)sender;     // クローズボタンと関連づけられたメソッド

@end

// 親画面へ戻るためのイベント用デリゲート
@protocol ChildSampleViewControllerDelegate <NSObject>
- (void) didFinishChildSample;              // クローズボタンが押されたとき呼び出すメソッド
@end
ChildSampleViewController.m
/* ..... */

// クローズボタンが押されたときのアクション
- (IBAction)pushCloseButton:(id)sender {
    [[self delegate] didFinishPreview];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/* ..... */

「StoryBoard 遷移前に遷移先のコントローラに値を渡すとき」のサンプルを参考に、
遷移前に親オブジェクトのポインタを子に渡しておき、デリゲートを呼び出せるようにしておく

ParentSampleViewController.m

@interface ParentViewController : UIViewController<ChildSampleViewControllerDelegate>

@end

@implementation ParentViewController

/* .... */

- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {

    // 対象セグエ以外ならここでリターン
    if(![[segue identifier] isEqualToString:@"OpenChild"])
        return;

    // 遷移先コントローラを取得
    ChildSampleViewController *childController
        = (ChildSampleViewController*)[segue destinationViewController];

    // 遷移元ポインタを渡しておく
    childController.delegate = self;
}


- (void) didFinishChildSample
{
    // ChildSampleViewのメソッドなりプロパティーを引っ張ってくる
}

@end
47
47
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
47

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?