我有一个 MYView 类(UIView 的子类),我将 MyView 作为子视图添加到我当前的 ViewController 中。MyView 有一个按钮,如果触发了 TouchUpInsideEvent,它会调用委托方法。我的问题是,当我点击按钮时,控件转到委托方法,但是如果我触发 TouchUpinsideEvent prgramaticaaly,它不会调用委托方法。这是我的代码:
我的视图.h
@protocol MyViewDelegate;
@interface MyView : UIView
@property(nonatomic,assign)id<MyViewDelegate> delegate;
@protocol MyViewDelegate <NSObject>
- (void)selctedOption:(MyView *)slideView withOption:(UILabel *)option withIndex: (NSInteger *)labelIndex;
@end
我的视图.m
toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
//adding target works fine it goes to the finishedDraggingVertical:withEvent method and from there I am calling the delegate method
[toggleButton addTarget:self action:@selector(finishedDraggingVertical:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);
toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:1.1 blue:0.3 alpha:0.2];
[toggleButton.layer setBorderWidth:10.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=10.0;
[self addSubview:toggleButton];
**//But If I fire the event programatically this doesn't work it goes to finishedDraggingVertical:withEvent but from there it does not go to the delegate method.**
[toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside]
MyViewController.h
@interface MyViewController : UIViewController <MyViewDelegate>
我的视图控制器.m
//这里我添加myView并实现委托方法
- (void)viewDidLoad
{
[super viewDidLoad];
MyView *myiew1=[[MyView alloc]init];
myiew1.delegate=self;
[self.view addSubview:slideView1];
}