LoginSignup
3
2

More than 5 years have passed since last update.

iOSメモ - tableviewとunwindSegueについて

Last updated at Posted at 2014-11-15

まずデモから
http://g.recordit.co/z1qKMnhtXm.gif

NavigationVCにrapしたfirstVCとsecondVCでcellに新しくデータを追加するデモです

1.firstVCにはBar Button Itemを入れるのであって、Buttonを入れようね(あとでsecondVCにpushで繋いでも動きません

スクリーンショット 2014-11-15 9.52.57.png

2.textFieldの定義は、SecondVC.mに書くのではなくSecondVC.hに書こう

@property (nonatomic, weak) IBOutlet UITextField *textField;

mに書いちゃうとbackToList内でsvc.textField.textって書くとエラー出るよね、辛い
また、hに@propertyではなく、

    @interface SecondViewController : UIViewController{
        IBOutlet UITextField *textField;
    }
    @end

のように書いても、svc.textField.textって書くとエラー出るよね、辛い

3.firstVCに以下を追加する

#pragma mark - firstVCのtableが乗る所に書く、firstVCからExitにて紐付いている
- (IBAction)backToList:(UIStoryboardSegue *)unwindSegue
{
if ([[unwindSegue identifier] isEqualToString:@"EditDoneSegue"]) {
    SecondViewController *svc = unwindSegue.sourceViewController;
    NSIndexPath *indexPathToInsert = [NSIndexPath indexPathForRow:0 inSection:0];
    [nameArray insertObject:svc.textField.text atIndex:indexPathToInsert.row];
    [table insertRowsAtIndexPaths:@[indexPathToInsert] withRowAnimation:UITableViewRowAnimationFade];
    }
}

(シンタックスハイライトちゃんと動かない、辛い)

4.ButtonからExitにcontrolを押して繋げる

そうすると、firstVCで定義したbackToListメソッドを繋ぐかどうか聞かれます

閉じた時に、backToListが動くってことですね

先ほど作ったUnwind segueにidentifierをつけます

スクリーンショット 2014-11-15 9.00.01.png

これで親から子の値渡しの代替が出来ます

3
2
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
3
2