我有一個MYView類(UIView的子類),我將MyView添加到我當前的ViewController中作爲子視圖。如果觸發TouchUpInsideEvent,MyView有一個調用委託方法的按鈕。我的問題是,當我在按鍵敲擊時進入的委託方法,但如果我火TouchUpinsideEvent prgramaticaaly它不調用委託method.Here是我的代碼:UITouchUpInside不調用委託方法
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];
}
你在哪裏分配代表? – Rakesh 2013-03-14 07:44:08
在我的視圖控制器 – user1787741 2013-03-14 08:17:41
你可以顯示MyViewDelegate的完整協議聲明,以及你在哪裏試圖調用委託? – Fogmeister 2013-03-14 08:26:57