23
25

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のsegueでパラメタ受け渡したった

Last updated at Posted at 2014-02-21

iOS7が出た以降、新規プロジェクトを起こすときは、
SBを使うようにしていて、その辺のメモ。

performSegueWithIdentifierする時に、
もっと色々と渡せたらいいのにー。

遷移方法

Storyboardから遷移先に引いたpush(例えば)の線にIDを設定し、
そのIDを使って、下記実装をすれば遷移可能

FirstViewController.m
- (void) openChatView
{
    [self performSegueWithIdentifier:@"openChatView" sender:self];
}

おっと本当に遷移していいかチェックしようぜ

FirstViewController.m
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier
                                  sender:(id)sender {

  // 仮にテーブルセルを選択して、次画面遷移するとしたら・・・
  NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

  // チェック対象の遷移はこれかな? segueの名前から、チャット系なのかな?
  if ([identifier isEqualToString:@"openChatView"]) {

    // 選択セルの情報はとれたかな? _chatListはNSArray的なやつで、isEmptyはnil & countチェックメソッドdeath!
    if (_chatList.isEmpty || ![_chatList objectAtIndex:indexPath.row]) {
      return NO;
    }
  }

  // いくぜ
  return YES;
}

遷移時のパラメータ受け渡し方法

渡す時は、UIViewControllerのメソッドをオーバーライドして、

FirstViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"openChatView"]) {
        SecondViewController *controller = segue.destinationViewController;
        controller.name = @"neiraza";
    }
}

受け取る側は、プロパティを用意しておけば。

SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSString *name;
@end
SecondViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
	NSLog(@"name %@", self.name);    
}
23
25
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
23
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?