[英]UITouchUpInside not calling delegate method
I have a class MYView(subclass of UIView) and I am adding MyView to My current ViewController as a subView. 我有一个类MyView(UIView的子类),并且正在将MyView作为子视图添加到我当前的ViewController中。 MyView has a button which calls a delegate method if TouchUpInsideEvent is fired.
MyView有一个按钮,如果触发了TouchUpInsideEvent,则该按钮将调用委托方法。 My problem is when I tap on button the control goes to delegate method but if I fire TouchUpinsideEvent prgramaticaaly it does not call the delegate method.Here is my code:
我的问题是,当我点击按钮时,控件转到委托方法,但是如果我以触发方式触发TouchUpinsideEvent,它不会调用委托方法。这是我的代码:
MyView.h 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 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 MyViewController.h
@interface MyViewController : UIViewController <MyViewDelegate>
MyViewController.m MyViewController.m
//Here I am adding myView and implementing the delegate method //这里我要添加myView并实现委托方法
- (void)viewDidLoad
{
[super viewDidLoad];
MyView *myiew1=[[MyView alloc]init];
myiew1.delegate=self;
[self.view addSubview:slideView1];
}
I know it's a workaround, but did you try the accepted answer for the below question: 我知道这是一种解决方法,但是您是否尝试了以下问题的可接受答案:
UIControl: sendActionsForControlEvents omits UIEvent UIControl:sendActionsForControlEvents省略了UIEvent
Also, check that the actions that you retrieve with [self actionsForTarget:target forControlEvent:controlEvent]
are consistent with the ones you set earlier in your code. 此外,请检查您使用
[self actionsForTarget:target forControlEvent:controlEvent]
检索到的动作是否与您先前在代码中设置的动作一致。 If not, then something is amiss. 如果不是,那么有些不对劲。
This is happening probably because you are setting the delegate after you have called: 之所以发生这种情况,可能是因为您在调用之后设置了委托:
[toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside];
I'm assuming you are adding the toggleButton in the init
or initWithFrame
method of your view and the above line of code is also inside the same. 我假设您在视图的
init
或initWithFrame
方法中添加toggleButton,并且上面的代码行也位于同一行中。
If you print out the delegate
object just before the delegate method call inside finishedDraggingVertical:withEvent
, you'll probably get Nil
. 如果您在
finishedDraggingVertical:withEvent
的委托方法调用之前打印出delegate
对象,则可能会得到Nil
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.