2013-03-14 52 views
0

我有一个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]; 
} 
+0

你在哪里分配代表? – Rakesh 2013-03-14 07:44:08

+0

在我的视图控制器 – user1787741 2013-03-14 08:17:41

+0

你可以显示MyViewDelegate的完整协议声明,以及你在哪里试图调用委托? – Fogmeister 2013-03-14 08:26:57

回答

0

这可能发生,因为你设置你叫后的代表:我假设你在你的视图的initinitWithFrame方法和上面的代码行中添加切换按钮

[toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside]; 

也在里面一样。

如果您在代理方法调用finishedDraggingVertical:withEvent之前打印出delegate对象,那么您可能会得到Nil

+0

是的,我明白了,谢谢 – user1787741 2013-03-14 08:46:18

1

我知道这是一个解决办法,但你尝试了下面的问题,接受的答案:

UIControl: sendActionsForControlEvents omits UIEvent

此外,请检查您使用[self actionsForTarget:target forControlEvent:controlEvent]检索的操作是否与您之前在代码中设置的操作一致。如果不是的话,那么有些麻烦。