更改 MGTwitterEngine 的委托
我已经使用 MGTwitterEngine 的扩展类通过 xAuth 设置并成功登录,我的问题是如果我想将其传递给另一个视图控制器,我如何更改委托类,因为它是某种弱引用
@interface MGTwitterEngine : NSObject <MGTwitterParserDelegate> {
__weak NSObject <MGTwitterEngineDelegate> *_delegate;
我最好包装这进入一个单例类并以这种方式传递,每次登录似乎都太过分了,或者我是否错过了一个非常明显的共享这个对象的方法
目前我已经向 MGTwitterEngine 添加了一个 setDelegate 方法,但感觉好像我我不必要地与框架作斗争
I have setup and successfully logged in via xAuth using an extended class of MGTwitterEngine, my question is if I want to pass this to another view controller, how can I change the delegate class, as it is some sort of weak reference
@interface MGTwitterEngine : NSObject <MGTwitterParserDelegate> {
__weak NSObject <MGTwitterEngineDelegate> *_delegate;
Am I best wrap this up into a singleton class and pass around that way, seems overkill to login in each time, or have I missed a painstakingly obvious way of sharing this object around
At the moment I have added a setDelegate method to the MGTwitterEngine but feel as though I am fighting the framework unnecessarily
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在多个对象之间共享引擎,那么您可能希望有一些其他对象/单例包装引擎并充当其唯一委托。如果您完成了数据库编程,那么请将其视为数据库连接——您可能不会让每个视图控制器创建自己的数据库连接。相反,您将创建某种由视图共享的数据管理器对象,并可能抽象出一些数据库内部结构。
如果不同的视图控制器处理不同的任务——比如登录、查找用户、查询消息等,那么包装器中的委托方法应该能够将响应传递到适当的视图控制器。
如果您有不同的视图控制器调用相同的方法(如果是这样,为什么?),您仍然可以将响应路由回相应的视图控制器。正如 MGTwitterEngine 文档所说,“每个 Twitter API 方法都会返回一个 NSString,它是该连接的唯一标识符。”您只需要传递一个对象(您的视图控制器)或一个块作为额外的每个包装方法的参数。当包装器发送响应时,您可以将 twitter id 字符串和此对象/块缓存在可变字典中,然后在处理响应时在缓存中查找连接 id。
If you're sharing the engine across multiple objects then you would want to have some other object/singleton wrap the engine and act as its sole delegate. If you've done database programming then think of it like a database connection -- you probably wouldn't have each view controller create its own database connection. Instead you'd create some sort of data manager object that is shared by the views and possibly abstracts away some of the DB internals.
If different view controllers handle different tasks -- like login, looking up users, querying messages, etc. then the delegate methods in your wrapper should be able to pass the responses along to the appropriate view controller.
If you have different view controllers calling the same methods (and if so, why?), you could still route responses back to the corresponding view controllers. As the MGTwitterEngine docs say, "Each Twitter API method returns an NSString which is a unique identifier for that connection." You would just need to pass an object (your view controller) or a block as an extra parameter to each of your wrapped methods. You can cache the twitter id string and this object/block in a mutable dictionary when your wrapper sends the response, then look up the connection id in the cache when it's time to handle the response.
事实上,你可以。
委托只不过是 MGTwitterEngine 中的一个变量。只需在下一个视图控制器中添加它的实例,添加适当的标头和实现调用即可。
初始化新的视图控制器集后:
然后调用 nextViewController。
当您返回到原始视图控制器时,不要忘记将委托设置回原始视图控制器(在 viewDidAppear 或 viewWillAppear 上)
希望有帮助...
祝你好运!
actually, you can.
The delegate, is nothing but a variable in the MGTwitterEngine. Just add a instance of it in the next view controller adding the proper header and inplementation calls.
after instatiating the new view controller set:
then call the nextViewController.
Do not forget to set the delegate back to the original view controller when you return to it (either on viewDidAppear or viewWillAppear)
Hope that helps...
Best Of luck!
在委托中使用 NSNotifications。
在您希望委托添加观察者的位置创建视图控制器。让 MGTwitterEngine 的委托方法发布通知。
Use NSNotifications in the delegate.
Make the view controller where you wish the delegate to be add an observer. Have the delegate method for MGTwitterEngine post the notification.