iOS: Communicating to parent view controller

2019-02-27 02:40发布

Parent view controller is a table of words. Child view segues on the screen and lets you play with that word. User can "flag" the word (which saves into user defaults). How should the parent table now find out that word has been flagged so we can display a pretty flag icon?

2条回答
祖国的老花朵
2楼-- · 2019-02-27 02:53

Using delegation is the best option. Make parent to be delegate of your child. No matter what data type you wanna use to pass, in your child VC, either your from your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath() if you only allow one word to be select at a time or a button press function if you allow multiple selection. Call your delegate method to receive those words send from child, then you can do whatever you want with it.

Hope this helps.

查看更多
萌系小妹纸
3楼-- · 2019-02-27 02:55

To pass data back from child to parent view controller you need to use Protocols and Delegates

See Passing Data Back section of this SO accepted answer for detailed explanation about passing information back and forth between view controllers

You can have a boolean in the child view controller.

You can set that boolean to true when the user "flags" it.

As shown above you can send back the value to the parent view controller whether it has been flagged or not.

Hope this helps.

查看更多
登录 后发表回答