[英]UITouchUpInside not calling delegate method
我有一个类MyView(UIView的子类),并且正在将MyView作为子视图添加到我当前的ViewController中。 MyView有一个按钮,如果触发了TouchUpInsideEvent,则该按钮将调用委托方法。 我的问题是,当我点击按钮时,控件转到委托方法,但是如果我以触发方式触发TouchUpinsideEvent,它不会调用委托方法。这是我的代码:
MyView.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
MyView.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>
MyViewController.m
//这里我要添加myView并实现委托方法
- (void)viewDidLoad
{
[super viewDidLoad];
MyView *myiew1=[[MyView alloc]init];
myiew1.delegate=self;
[self.view addSubview:slideView1];
}
我知道这是一种解决方法,但是您是否尝试了以下问题的可接受答案:
UIControl:sendActionsForControlEvents省略了UIEvent
此外,请检查您使用[self actionsForTarget:target forControlEvent:controlEvent]
检索到的动作是否与您先前在代码中设置的动作一致。 如果不是,那么有些不对劲。
之所以发生这种情况,可能是因为您在调用之后设置了委托:
[toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside];
我假设您在视图的init
或initWithFrame
方法中添加toggleButton,并且上面的代码行也位于同一行中。
如果您在finishedDraggingVertical:withEvent
的委托方法调用之前打印出delegate
对象,则可能会得到Nil
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.