2009-09-05 121 views
0

我是一個有關iPhone上的通知小新手。觸摸通知結束

我所試圖做的是這樣的:我有使用基於一個UIImageView的類的對象。這個類有它自己的touchbegan,touchesended的實現等

這是可能的主程序知道當用戶從對象提起他的手指?換句話說,是否有可能從類創建一個通知給主應用程序的視圖控制器(使用類),以便它會觸發一個方法?該方法應該在主應用程序上,而不是在類上。

這可能嗎?你能給個例子嗎?

感謝您的任何幫助。

回答

1

-touchesEnded:,添加類似下面的語句(改變handleTouchesEndedNotification獨特的名稱要使用的通知):

[[NSNotificationCenter defaultCenter] postNotificationName:@"handleTouchesEndedNotification" object:nil userInfo:nil]; 

在你的主,應用程序委託或其他類,添加以下語句其中類被初始化,來監聽你在上面使用的唯一名稱的通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMyNotification:) name:@"handleTouchesEndedNotification" object:nil]; 

而且到你的類中,添加方法與邏輯:

- (void) handleMyNotification:(NSNotification *)notification { 
    // do stuff... 
} 

如果要將數據與通知一起傳遞,請創建一個NSDictionary對象並將通知的userInfo變量設置爲此字典對象。

+0

謝謝!!!!!!你是男人! – SpaceDog 2009-09-05 11:16:10