[英]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.