[英]Can't send a UIButton event to other view controller correctly
有人可以提供一些示例和正式模式用于UIViewController
之间的一对一事件传输吗? 我认为NSNotificationCenter
不适用于此用例,因为它基于事件总线和广泛状态更改的广播模式,因此应将其用于一对多传输。 我知道, KVO
是不是适用于这种情况在所有太,因为它通常用于传统的模型和控制器层之间的通信MVC
境界。 所以现在我只知道一种一对一事件传输的方法:委托模式。 但是也许会有更优雅,更simple
不 easy
解决方案。
例如:
在视图中,操作被发送到:
@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];
在视图中,该操作将从以下位置发送:
#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 MyViewWithButtonDelegateProtocol<NSObject>
-(void)myButtonAction:(id)sender; @end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.