簡単!UINavigationController間での値の受け渡し

Sponsored Link

 

 

はい、やまさきです!

今までは通知(NSNotificationCenter)とか使っていましたが簡単な方法を見つけました!
なのでその簡単な方法を説明します。
まずはNavigationControllerを使うのでAとB、二つのビューを用意して下さい。xibの使い方いまいちわからないのでもちろんコードべた書きです。
ルートビューをAに設定したら適当にボタンでも作ってその中でpushViewControllerを行います。
今日の記事はここまでを自力で出来る方限定です。
ここまでの行程で分からない事があればグーグル先生に聞いて下さい。
さて、ここからが今日の本題。


私のテストプロジェクトを元に説明します。

A == FirstViewController
B == SecondViewController とする

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interfaceFirstViewController ()
@end

@implementation FirstViewController
{
    UILabel *firstViewLabel;
    UIButton *firstViewButton;
    NSString *firstString;
}

- (void)viewDidLoad
{
    [superviewDidLoad];
    // Do any additional setup after loading the view.
    [selfviewSetting];
}

- (void)didReceiveMemoryWarning
{
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewSetting
{
    // ラベル設定
    firstViewLabel = [[UILabelalloc]initWithFrame:CGRectMake(125, 100, 320, 100)];
    firstViewLabel.text = @"First View";
    [self.viewaddSubview:firstViewLabel];

    // タイトル設定
    self.navigationItem.title = @"First View";
    // ボタンの設定
    firstViewButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
    firstViewButton.frame = CGRectMake(100, 300, 130, 100);
    [firstViewButtonsetTitle:@"Button to Second" forState:UIControlStateNormal];
    [firstViewButtonaddTarget:selfaction:@selector(navigationButton:) forControlEvents:UIControlEventTouchDown];
    [self.viewaddSubview:firstViewButton];

    // 遷移先に渡す値(今回は文字列を渡します
    firstString = @"遷移元で作成した変数";
}

// ボタンを押した時の動作
- (void)navigationButton:(UIButton *)btn
{
    // 移動先のViewクラスの初期化
    SecondViewController *secondView = [[SecondViewControlleralloc]init];

    // 遷移先の変数に遷移元の値を渡す
    [secondView setSecondString:firstString];
    // pushViewController:画面遷移
    [self.navigationController pushViewController:secondView animated:YES];
}
@end

続いてSecondViewController.h

#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController <UINavigationControllerDelegate,UINavigationBarDelegate>
{
    UILabel *secondLabel;
}

// 遷移元から値を受ける変数を作る
@property(nonatomic)NSString *secondString;
@end

次にSecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"

@interfaceSecondViewController ()
@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [superviewDidLoad];
    // Do any additional setup after loading the view.
    [selfviewSetting];
}

- (void)didReceiveMemoryWarning
{
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewSetting
{
    // ラベル設定
    secondLabel = [[UILabelalloc]initWithFrame:CGRectMake(70, 100, 320, 100)];
    secondLabel.text = _secondString;
    [self.viewaddSubview:secondLabel];

    // タイトル設定
    self.navigationItem.title = @"Second View";
}
@end

以上のハイライト部分が今回のポイントです
FirstViewController.hは何も触ってないので記載していません。あしからず。

ざっと説明すると遷移元の変数を受ける側で受ける為に作成した変数に渡してやる。
それを遷移先で受け取ると言う寸法。
ちなみにsetSecondStringは命名規則があるみたいなので作成した変数の頭を大文字にして下さい
分からない方はじっくりコードを眺めて欲しい。
穴があくほど眺めれば必ず理解出来ます。
ちなみに実際に動くコードをコピペしているので必ず動きます。
よくコードを乗せてて実際に動かないとかあると思うので動かない場合はコメント欄に書き込みお願いします。
即時修正、即時反映します!

ばいちゃ!

 

 

Sponsored Link
ツイートツイート

簡単!UINavigationController間での値の受け渡し

2 thoughts on “簡単!UINavigationController間での値の受け渡し

  • 2014年11月27日 at 11:13
    Permalink

    Tarouさんへ
    現在、クラス間遷移が何度か必要なアプリを作成中です。AppDelegeteを使ったデータの受渡しを使っています。
    もっと、簡便な方法が無いか探しているところで、出会いました。簡潔明瞭で分かりやすいですね。
    大変参考になりました、利用させていただきます。作成期日が記述されているので安心でした。
    2014年11月27日 砂川(リタイア4年目です)

    Reply
    • 2014年11月30日 at 13:25
      Permalink

      砂川さんへ

      わざわざコメントありがとうございます。
      簡潔明瞭などとお褒めの言葉を頂き嬉しく思います。
      これからも精進させて頂きます。

      2014年11月30日 yamasaki

      Reply

tarou yamasaki へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です