xcode Objective-c 中的多个代表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10358917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple delegates in objective-c
提问by DoS
i have been working on moving one of my apps away from the "shared appdelegate" process which seems to be frowned up, despite its over whelming use. i have been attempting to setup protocol methods for what i want to do but am having zero luck. my question is, can you even have lets say a single viewcontroller send delegate requests to multiple classes? from what im finding out it doesn't seem like you can. which doesn't make sense because i thought that was the whole point of delegates and protocols with mvc. now just to clarify, i know you can have a single viewcontroller act as the delegate for multiple other viewcontrollers. but that's not what i am asking. for a simple example, lets say you have apples flip-utility template. the "done" button just calls a delegate method to the mainvc to dismiss it. now lets say we added a new class called...
我一直在努力将我的一个应用程序从“共享 appdelegate”进程中移开,尽管它被过度使用,但它似乎不受欢迎。我一直在尝试为我想做的事情设置协议方法,但运气为零。我的问题是,您甚至可以说单个视图控制器向多个类发送委托请求吗?从我发现的情况来看,您似乎无法做到。这没有意义,因为我认为这就是 MVC 的委托和协议的全部意义所在。现在只是为了澄清,我知道您可以让一个视图控制器充当多个其他视图控制器的委托。但这不是我要问的。举一个简单的例子,假设你有苹果翻转实用程序模板。“完成”按钮只是向 mainvc 调用一个委托方法来关闭它。
@interface NewClass : NSObject <TheOtherDelegate>
and it had a delegate method...
它有一个委托方法......
- (void)doSomething
{
NSLog(@"The Delegate did something...");
}
can we have a button on the flipsideviewcontroller, that we wanted to call that delegate method, but still keep the "done" button call to the delegate method on the mainviewcontroller that dismisses it?
我们可以在flipsideviewcontroller 上有一个按钮,我们想调用该委托方法,但仍然保留对mainviewcontroller 上的委托方法的“完成”按钮调用以解除它?
that being said, i put together a quicky project just to see if it would work and it doesn't. i came across an "answer" that says you have to instantiate the class first you want to be the delegate...
话虽如此,我整理了一个快速的项目只是为了看看它是否有效,而它无效。我遇到了一个“答案”,说你必须先实例化你想成为代表的类......
NewClass *myDelegate = [NewClass alloc] init]
[fillInMethodHere setDelegate:myDelegate];
not sure why it got a correct answer check, because needless to say it doesn't work. is there something i am missing? i scoured ib to see if there is some "delegate" connection somewhere but i couldn't find anything.
不知道为什么它得到了正确的答案检查,因为不用说它不起作用。有什么我想念的吗?我搜索了 ib 以查看某处是否有一些“委托”连接,但我找不到任何东西。
on a side note, as i was working in my working project, i read a suggestion about removing the #import
and adding @class
. again, that broke all kinds of things. the strange thing is before doing that, what i had so far was working and building fine. when i removed the new @class
and un-commented the #import
. xcode all of a sudden gave me an error "cannot find protocol deceleration for..." but yet, it worked seconds earlier. i would up having to remove the protocol code and re-add it for it to work again. very starge.
附带说明一下,当我在我的工作项目中工作时,我阅读了关于删除#import
和添加@class
. 再次,这打破了各种各样的东西。奇怪的是,在这样做之前,我到目前为止所做的工作和构建都很好。当我删除新的@class
和未注释的#import
. xcode 突然给了我一个错误“找不到...的协议减速”但是,它在几秒钟前就起作用了。我将不得不删除协议代码并重新添加它以使其再次工作。非常明星。
any help would be appreciated. everything iv read in docs, google, stack, etc that say something should work, don't in an actual project.
任何帮助,将不胜感激。iv 在文档、谷歌、堆栈等中阅读的所有内容都表明某些东西应该起作用,而不是在实际项目中。
回答by Hyman Lawrence
A "delegate" isn't some fancy object. It's simply a synthesized property of type id called delegate
. If you wanted to, you could have an arbitrary number of properties that all conformed to the same protocol. Then when you wanted to issue a callback, you would just address all of them:
“委托”不是什么花哨的对象。它只是一个名为 id 类型的综合属性delegate
。如果您愿意,您可以拥有任意数量的符合相同协议的属性。然后,当您想发出回调时,您只需解决所有这些问题:
[self.mydelegateA doSomething];
[self.mydelegateB doSomething];
etc.
等等。
You could also have an NSMutableArray
property that you could add objects to, and then use [self.myMutableArrayOfDelegates makeObjectsPerformSelector:@selector(doSomething)]
.
您还可以拥有一个NSMutableArray
属性,您可以向其中添加对象,然后使用[self.myMutableArrayOfDelegates makeObjectsPerformSelector:@selector(doSomething)]
.
Finally, there's always the route of NSNotificationCenter
(not to be confused with push notifications) is a class that provides an inter-object messaging system. Many objects can register for a message that any other object can send.
最后,总是有一个路径NSNotificationCenter
(不要与推送通知混淆)是一个提供对象间消息传递系统的类。许多对象可以注册任何其他对象可以发送的消息。
Please see the Apple's documentation for more information. Click Here.
有关更多信息,请参阅 Apple 的文档。点击这里。
Regardless of the fact that this is OS X documentation, it's still quite good at explaining things visually: click here.
尽管这是 OS X 文档,但它仍然非常擅长以视觉方式解释事物:单击此处。
Here's an example of simply changing the name of the delegate property: click here
这是一个简单地更改委托属性名称的示例:单击此处
And here's an example of adding another protocol and a second delegate: click here
这是添加另一个协议和第二个委托的示例:单击此处
Finally, here's an example that builds on the previous two and has a third delegate that also conforms to the same protocol: click here
最后,这里有一个示例,它建立在前两个基础上,并具有第三个也符合相同协议的委托:单击此处