如何在 Obj-C/Cocoa 中使用委托向后/向上传递数据视图控制器链

发布于 2024-10-08 15:12:49 字数 1010 浏览 0 评论 0 原文

我知道 Apple 开发文档、我拥有的其他书籍以及 stackoverflow 等资源中都有很多关于代表主题的内容。但我还是没明白。

我最近在斯坦福大学的CS193P Winter 2010系列中观看了关于导航视图控制器等的讲座,在那场讲座中他们谈到了在一堆视图控制器上向前传递数据,这很简单。但他们简短地提到,您最好使用委托/协议“向后”传递数据(例如,从详细视图控制器到列表视图控制器),但他们没有进行演示或发布示例代码。

我已经阅读并搜索了这个确切场景的示例,因此我可以了解委托/协议的使用,但找不到它。这是我正在使用的一些伪代码。它应该实现“向后”传递数据吗?

myListController : UIViewController <SetDataInParent> {
 // when pushing detail controller onto stack,
 // set DetailController delegate = self
}

myDetailController : UIViewController {
//header file
@protocol SetDataInParent <NSObject>
- (void)willSetValue:(*NSString);
@end

@interface myDetailController {
id <SetDataInParent> delegate;
}
@end

// class/m file 
@implementation
@synthesize delegate;
- (void)willSetValue:(*NSString) {
// code here that would take argument
// from detail controller and set
// a value or text field to that
// argument in list controller
}
// send message to list controller class
- [delegate willSetValue:string];
@end
}

I know there is plenty on the subject of delegates in the Apple dev documentation, as well as other books I have, and in resources like stackoverflow and others. But I'm still not getting it.

I recently watched the lecture on Navigation View Controllers, etc. in Stanford's CS193P Winter 2010 series, and in that lecture they talk about passing data forward on a stack of view controllers, which is easy. But they made a brief mention that you'd ideally use a delegate/protocol to pass data "backwards" (from detail view controller to list view controller, for example), but they didn't do a demo or post sample code.

I've read and searched for a sample of this exact scenario so I can wrap my head around that use of delegate/protocol, but can't find it. Here's some pseudo-code I'm playing with. Should it achieve passing the data "backwards"?

myListController : UIViewController <SetDataInParent> {
 // when pushing detail controller onto stack,
 // set DetailController delegate = self
}

myDetailController : UIViewController {
//header file
@protocol SetDataInParent <NSObject>
- (void)willSetValue:(*NSString);
@end

@interface myDetailController {
id <SetDataInParent> delegate;
}
@end

// class/m file 
@implementation
@synthesize delegate;
- (void)willSetValue:(*NSString) {
// code here that would take argument
// from detail controller and set
// a value or text field to that
// argument in list controller
}
// send message to list controller class
- [delegate willSetValue:string];
@end
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2024-10-15 15:12:49

委托只是您当前要向其发送一条或多条消息的实例之外的任何其他 Objective-C 实例。您指定的类型 (id ) 表示它是任何符合 SetDataInParent 协议的 Objective-C 类型(我没有在您的代码中看到该协议的定义,但您可以在其他地方拥有它。)

当“向后”移动时,通常会在创建子视图时创建两个对象之间的委托链接。因此,在列表视图控制器创建详细视图控制器时,前者应将后者中的委托设置为 self。然后,详细视图控制器可以使用该委托指针将消息发送到列表视图控制器,可以直接通过 willSetValue: 或间接(通过 performSelector:withObject:)。使用performSelector:withObject:时,通常最好先在委托上调用respondsToSelector:,以确保在对象不响应时不会抛出异常那个消息。

A delegate is simply any other Objective-C instance other than the one you are currently to which you will be sending one or more messages. The type you specified (id <SetDataInParent) reads that it's any Objective-C type that conforms to the SetDataInParent protocol (which I don't see defined in your code, but you could have it elsewhere.)

When going "backwards" typically the delegate link between the two objects is made at the time the child view is created. Therefore at the point the list view controller creates the detail view controller the former should set the delegate in the latter to self. The detail view controller then can use that delegate pointer to send messages to the list view controller, either directly via e.g., willSetValue: or indirectly (via performSelector:withObject:.) When using performSelector:withObject: it is generally a good idea to call respondsToSelector: on the delegate first to make sure you won't throw an exception should the object not respond to that message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文