split view controller update table view

我的未来我决定 提交于 2019-12-11 11:38:46

问题


I am using Split View Controller. For master I have Navigation Controller with Table View Controller as a root view. For details I have Navigation Controller with Custom View Controller as a root view. From master I am selecting tableView row and displaying row details in details view. From details view i can edit details of this row using another View Controller with modal segue. The question is: How can I refresh tableView (master) after saving changes in details editing view (modal). When I tap save button the -(IBAction) unwindEditRow:(UIStoryboardSegue *)segue (of details) is triggered.


回答1:


You have a lot of ways to send messages to ViewController when particular event has happened.

The first method: Use delegation pattern. What is delegate? Delegation is a clean and simple way of connecting objects and helping them to communicate with other. In other words, delegation is a dating service for Objective-C objects. :) There are some useful links: Basic concepts in Objective-C, Writing your own custom delegate Here is the way of declaring your new protocol:

@protocol DetailViewControllerDelegate <NSObject>

-(void)itemHasBeenChanged:(id)edittedObject;

@end

In your DetailViewController declare your future delegate:

@property (weak,nonatomic) MasterViewController <DetailViewControllerDelegate> *delegate;

Implement itemHasBeenChanged: method in MasterViewController.m:

  -(void)itemHasBeenChanged:(id)edittedObject{
    //editting logic goes here
  }

Tell our class to implement the DetailViewControllerDelegate protocol so that it knows which functions are available to it by line:

@interface MasterViewController : UITableViewController <DetailViewControllerDelegate>

After all these steps, you can call method in your DetailViewController whenever you want by:

[self.delegate itemHasBeenChanged:yourObject];

Here is my example code on github

Second way

We can use NSNotificationCenter as an alternative to custom protocols. Making custom protocols and registering methods is hard to do in a big project and NSNotificationCenter can free us from that burden. The main approach used with NSNotificationCenter is that any object can send a notification to the notification center and at the same time any other object can listen for notifications on that center.



来源:https://stackoverflow.com/questions/20105827/split-view-controller-update-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!