まずデモから
http://g.recordit.co/z1qKMnhtXm.gif
NavigationVCにrapしたfirstVCとsecondVCでcellに新しくデータを追加するデモです
1.firstVCにはBar Button Itemを入れるのであって、Buttonを入れようね(あとでsecondVCにpushで繋いでも動きません
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をつけます
これで親から子の値渡しの代替が出来ます