2013-04-04 18 views
0

有人可以提供一些示例和正式模式,用於在UIViewController之間進行一對一事件傳輸嗎?我認爲NSNotificationCenter不適用於此用例,因爲它基於事件總線和廣播模式進行廣泛的狀態更改,因此應該用於一對多傳輸。我知道KVO在這種情況下不適用於,所有也是如此,因爲它通常用於經典MVC領域中的模型和控制器層之間的通信。所以現在我只知道一種一對一的事件傳輸方式:委託模式。但可能會有更優雅和simplenoteasy解決方案。無法正確地向其他視圖控制器發送UIButton事件

+0

需要使用代表.. – lakesh 2013-04-04 17:24:13

+0

即使協議也可以使用。 – 2013-04-04 17:25:20

回答

3

例如:

在視圖的動作發送到:

#import "MapView.h" 

@interface MapViewController : UIViewController<MapViewDelegate> 
{ 

} 

.m 

MapView *map = [[MapView alloc] init]; 
map.delegate = self; 

-(void)MapImageButtonClicked:(UIButton*)sender 
{ 
//implement the necessary functionality here 
} 

@protocol MapViewDelegate <NSObject> 

@required 

-(void)MapImageButtonClicked:(UIButton*)sender; 

@end 

@interface MapView : UIView 
{ 
    UIButton *mapButton; 
    id   mapViewDelegate; 
} 

@property(nonatomic,retain)  id    mapViewDelegate; 
@property(nonatomic,retain)  UIButton  *mapButton; 

的.m

[mapButton addTarget:self.delegate action:@selector(mapImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

在視圖的動作將從發送

希望你得到我數據包絡分析。請根據您的情況實施。

0

你可以把這個方法在協議中您的視圖界面:

@protocol MyViewWithButtonDelegateProtocol<NSObject> 

-(void)myButtonAction:(id)sender; @end 
  • 你把NSObject的或UIView的類型稱爲委託一個新的屬性在視圖 具有按鈕。
  • 您讓您的視圖必須處理符合該協議的動作,並且它在視圖中指定委託人屬性爲 self。
  • 你在你的視圖控制器實現中實現myButtonAction。
  • 現在你只需做[myButton setTarget:delegate action:@selector(myButtonAction :) forControlEvents:UIControlEventTouchUpInside] ;.
相關問題