更改MGTwitterEngine的委托 [英] Change the delegate of MGTwitterEngine

查看:139
本文介绍了更改MGTwitterEngine的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置并使用扩展类MGTwitterEngine通过xAuth成功登录,我的问题是如果我想将其传递给另一个视图控制器,我该如何更改委托类,因为它是某种弱引用

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

目前我已经向MGTwitterEngine添加了一个setDelegate方法,但感觉好像我在战斗不必要的框架

At the moment I have added a setDelegate method to the MGTwitterEngine but feel as though I am fighting the framework unnecessarily

推荐答案

如果你在多个对象之间共享引擎那么你会想要一些其他的对象/单例包装引擎并作为其唯一代表。如果您已经完成了数据库编程,那么可以将其视为数据库连接 - 您可能不会让每个视图控制器创建自己的数据库连接。相反,你会创建一些由视图共享的数据管理器对象,并可能抽象出一些数据库内部。

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.

如果您有不同的视图控制器调用相同的方法(如果是,为什么?),您仍然可以将响应路由回相应的视图控制器。正如MGTwitterEngine文档所说,每个Twitter API方法返回一个NSString,它是该连接的唯一标识符。您只需要传递一个对象(您的视图控制器)或一个块作为额外的每个包装方法的参数。当包装器发送响应时,您可以将twitter id字符串和此对象/块缓存在可变字典中,然后在处理响应时查找缓存中的连接ID。

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的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆