在更高層次上,我將創建一個具有確切的參數/名稱(甚至可能是自我和_cmd)的C++基類。在某處,您需要創建這些內容,並建立從管理器到所查詢對象的連接。
objc對象可以持有指向其C++實現/接收器的指針,也可以使用索引,或者可以使用self和散列/映射方法。
因此,管理員可以保存事件接收器的集合,然後UIView將實現該方法,該方法會將消息轉發給管理器來解析命令(您如何處理命令/事件?) 。或者,如果你想要的只是實現,那麼只需使用ObjectiveC++並將事件接收器/處理器作爲ivar添加到UIResponder。
增加:爲'然後只是使用ObjectiveC++並添加事件接收器/處理器作爲ivar到UIResponder'添加了一個快速的僞代碼說明。
// Mr.Gando:
// To answer your question about what do I need to do with events,
// for example I need to subscribe a button to the manager, so when
// it get's touched, a method "Fire" is called... just an example,
// but I think your answer is good. Does this Manager have to be
// thread safe ?
/////////////////////////////////////////////////////////////////////
/* not declared in event_manager_t's scope, so that objc only files may declare members without error - if all your code is objc++, then relocate */
class mon_event_manager_event_handler_t {
/* ... */
void touchesBegan(id sender, NSSet * touches, UIEvent * event) {
assert(sender && touches && event);
assert(this->getResponder() && this->getResponder() == sender);
if (this->getResponder() && this->getResponder() == sender && this->isSubscribed() && this->isInterestedInTouchesBeganEvents()) {
SharedEventManager().touchesBegan(this, sender, touches, event);
}
}
private:
UIResponder * responder_;
UInt32 flags_EventsOrActionsOfInterest_;
bool isSubscribed_;
};
/** implement as singleton */
class event_manager_t {
/* ... */
void touchesBegan(handler_t* const handler, id sender, NSSet * touches, UIEvent * event) {
this->fire();
}
void fire() {
NSLog(@"Fire()");
/* you can message anything here, just realize that events may not occur on the recipient's work thread */
}
static mon_event_manager_event_handler_t* CreateNextHandler(UIResponder * responder) {
/* SharedEventManager() must guard its data here */
mon_event_manager_event_handler_t* result(SharedEventManager().createOrReuseHandler());
result->setResponder(responder);
SharedEventManager().registerNewHandler(handler);
return result;
}
static void RemoveHandler(mon_event_manager_event_handler_t* handler) {
/* SharedEventManager() must guard its data here */
SharedEventManager().removeHandler(handler);
}
};
/** @return the event_manager_t singleton object */
event_manager_t& SharedEventManager();
/////////////////////////////////////////////////////////////////////
struct mon_event_manager_event_handler_t;
@interface MonResponder
{
mon_event_manager_event_handler_t* handler_;
}
@end
/////////////////////////////////////////////////////////////////////
@implementation MonResponder
/* ... */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
assert(handler_ && "TODO: create the handler");
handler_->touchesBegan(self, touches, event);
/* ... */
}
@end
/////////////////////////////////////////////////////////////////////
對於多級設計,給定的規範有點模糊,不是嗎? – justin 2009-12-03 06:23:27