拆分视图控制器更新表视图 [英] split view controller update table view
问题描述
我正在使用分屏控制器.对于 master,我将带有 Table View Controller 的 Navigation Controller 作为根视图.有关详细信息,我将带有自定义视图控制器的导航控制器作为根视图.从 master 我选择 tableView 行并在详细信息视图中显示行详细信息.从详细信息视图中,我可以使用另一个带有模态转场的视图控制器来编辑该行的详细信息.问题是:如何在详细信息编辑视图(模态)中保存更改后刷新 tableView(主).当我点击保存按钮时,-(IBAction) unwindEditRow:(UIStoryboardSegue *)segue
(详细信息)被触发.
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.
推荐答案
当特定事件发生时,您有很多方法可以向 ViewController 发送消息.
You have a lot of ways to send messages to ViewController when particular event has happened.
第一种方法:使用委托模式.什么是委托?委托是一种连接对象并帮助它们与其他对象通信的干净而简单的方法.换句话说,委托是 Objective-C 对象的约会服务. :) 有一些有用的链接:Objective-C 中的基本概念,编写自己的自定义委托以下是声明新协议的方式:
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
在您的 DetailViewController 中声明您的未来委托:
In your DetailViewController declare your future delegate:
@property (weak,nonatomic) MasterViewController <DetailViewControllerDelegate> *delegate;
在 MasterViewController.m 中实现 itemHasBeenChanged:
方法:
Implement itemHasBeenChanged:
method in MasterViewController.m:
-(void)itemHasBeenChanged:(id)edittedObject{
//editting logic goes here
}
告诉我们的类实现 DetailViewControllerDelegate
协议,以便它知道哪些函数可以按行使用:
Tell our class to implement the DetailViewControllerDelegate
protocol so that it knows which functions are available to it by line:
@interface MasterViewController : UITableViewController <DetailViewControllerDelegate>
完成所有这些步骤后,您可以随时通过以下方式调用 DetailViewController 中的方法:
After all these steps, you can call method in your DetailViewController whenever you want by:
[self.delegate itemHasBeenChanged:yourObject];
第二种方式
我们可以使用 NSNotificationCenter 作为自定义协议的替代方案.制作自定义协议和注册方法在一个大项目中很难做到,而 NSNotificationCenter 可以让我们摆脱这种负担.与 NSNotificationCenter 一起使用的主要方法是任何对象都可以向通知中心发送通知,同时任何其他对象都可以在该中心监听通知.
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.
这篇关于拆分视图控制器更新表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!