简体   繁体   中英

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 has a button which calls a delegate method if TouchUpInsideEvent is fired. 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:

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

//Here I am adding myView and implementing the delegate method

- (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

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. 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.

If you print out the delegate object just before the delegate method call inside finishedDraggingVertical:withEvent , you'll probably get Nil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM