2013-03-14 193 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]檢索的操作是否與您之前在代碼中設置的操作一致。如果不是的話,那麼有些麻煩。