【问题标题】:UITouchUpInside not calling delegate methodUITouchUpInside 不调用委托方法
【发布时间】:2013-03-14 06:50:30
【问题描述】:

我有一个类 MYView(UIView 的子类),我将 MyView 添加到我当前的 ViewController 作为一个子视图。 MyView 有一个按钮,如果触发了 TouchUpInsideEvent,它会调用委托方法。我的问题是当我点击按钮时,控件转到委托方法,但是如果我触发 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];
}

【问题讨论】:

  • 你在哪里分配代表?
  • 在我的视图控制器中
  • 您能否显示 MyViewDelegate 的完整协议声明以及您要在哪里调用代理?
  • 不是[self.view addSubview:myView1]吗?

标签: ios ios5 ios4


【解决方案1】:

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

UIControl: sendActionsForControlEvents omits UIEvent

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

【讨论】:

    【解决方案2】:

    这可能是因为您在调用后设置了委托:

     [toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    

    我假设您在视图的initinitWithFrame 方法中添加了toggleButton,并且上面的代码行也在同一行中。

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

    【讨论】:

      猜你喜欢
      • 2012-09-22
      • 2014-11-21
      • 2017-02-14
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多