如何将数据从UITableViewCell子类传递到iOS中的RootViewController?

时间:2012-06-27 18:45:23

标签: iphone ios design-patterns user-interface

我已经创建了UITableViewCell的自定义子类。在那里,我允许用户编辑UITextView中的一些文本。该单元属于弹出视图中的表,该视图是根视图的子视图。现在,我需要将表格单元格中编辑的文本以及单元格中的其他数据传递给根视图。这是因为,必须将此编辑的数据发送到webview,webview也是根视图的子视图。

我考虑了以下方法。一种是使弹出视图成为自定义单元视图的委托,将根视图作为弹出视图的delgate,并通过委托方法调用传递数据。这可能会使设计过于复杂。第二种方法是在创建单个类时在webview中存储对webview的引用,并直接从单元格更新它。但这会使封装所有这些类都试图完全没有意义。

最佳方法是什么?除了我建议的两个以外还有什么方法吗?

1 个答案:

答案 0 :(得分:3)

使用NSNotificationCenter,请参阅Class Reference

创建通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:@"doSomething" object:nil];

发布通知

NSString * testString = @"Testing NSNotificationCenter";
[[NSNotificationCenter defaultCenter] postNotificationName: @"doSomething" object: testString];

处理通知

- (void)doSomething:(NSNotification *)notification
{
    NSString * someString = [notification object];
    NSLog(@"String Passed from NSNotificationCenter: %@", someString);
}