UITouchUpInside没有调用委托方法

时间:2013-03-14 06:50:30

标签: ios ios5 ios4

我有一个类MYView(UIView的子类),我将MyView添加到我当前的ViewController作为子视图。如果触发了TouchUpInsideEvent,MyView有一个调用委托方法的按钮。我的问题是,当我点击按钮时控件转到委托方法,但如果我触发TouchUpinsideEvent prgramaticaaly它不会调用委托方法。这是我的代码:

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];
}

2 个答案:

答案 0 :(得分:1)

我知道这是一种解决方法,但您是否尝试了以下问题的接受答案:

UIControl: sendActionsForControlEvents omits UIEvent

另外,请检查使用[self actionsForTarget:target forControlEvent:controlEvent]检索的操作是否与您之前在代码中设置的操作一致。如果没有,那么就会出现问题。

答案 1 :(得分:0)

发生这种情况可能是因为您在调用后设置了委托:

 [toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside];

我假设您要在视图的initinitWithFrame方法中添加toggleButton,上面的代码行也在同一个内部。

如果您在delegate内的委托方法调用之前打印出finishedDraggingVertical:withEvent对象,则可能会获得Nil