2012-10-03 113 views
0

我有一个视图控制器,其中包含嵌套UIView。我想为每个添加的子视图添加一个水龙头监听器。但是当我点击子视图时,我遇到了一个SIGABRT错误。设置自定义UIView内循环UITapGestureRecognizer

这里是我的代码:

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    //set container 
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 320, 420)]; 
    container.backgroundColor = [UIColor blueColor]; 

    //create new subview within container. I call it "card" 
    for (int i=0; i<5; i++) 
    { 
     //create card 
     UIView *card = [[UIView alloc] initWithFrame:CGRectMake(10, 35 + (65 * i), 300, 45)]; 
     card.backgroundColor = [UIColor greenColor]; 

     //set tap event with action selector = cardRowTapped 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped)]; 
     [card addGestureRecognizer:tap]; 

     //add subview 
     [container addSubview:card]; 
    } 

    //add subview to self 
    [self.view addSubview:container]; 

} 

,这里是我的自来水处理程序代码

- (void) cardRowTapped:(UITapGestureRecognizer *)gr { 
    NSLog(@"hello"); 
} 

控制台输出:

2012年10月3日13:23:37.173 MyProject的[5167:707] - [MyViewController cardRowTapped]:无法识别的选择器发送到实例0x2cd810

2012年10月3日13:23:37.179 MyProject的[5167:707] *终止应用程序由于 未捕获的异常 'NSInvalidArgumentException',原因: “ - [MyViewController cardRowTapped]:无法识别的选择发送到 实例0x2cd810 “

*第一掷调用堆栈:(0x314b888f 0x377f6259 0x314bba9b 0x314ba915 0x31415650 0x30c45637 0x30bd5d65 0x30e06479 0x30b51f55 0x30b50aa3 0x30b5d7e9 0x30b5d627 0x30b5d1f5 0x30b43695 0x30b42f3b 0x3348922b 0x3148c523 0x3148c4c5 0x3148b313 0x3140e4a5 0x3140e36d 0x33488439 0x30b71cd5 0x76 e8d 0x76e28)终止叫抛 例外(lldb)

任何想法为什么会发生这种情况?

+1

你能提供在控制台中的错误? –

回答

0

你的方法需要被用手势传递一个成员变量,所以改变敲击手势的方法类似下面,

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)]; 

,并检查了它应该,如果你不使用ARC工作,还有一两件事,然后在使用后释放对象。

+0

谢谢。这是行得通的。你救了我的命。但是cardRowTapped中的':'是什么意思? –

+0

如果你的方法有/需要参数,那么在你的选择器中,你必须在方法名后面提供':'。 – vishy

0

试试这个代码,当您添加的姿态给像波纹管的委托..

UIView *card = [[UIView alloc] initWithFrame:CGRectMake(10, 35 + (65 * i), 300, 45)]; 
     card.backgroundColor = [UIColor greenColor]; 

     //set tap event with action selector = cardRowTapped 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)]; 
     [tap setNumberOfTapsRequired:1]; 
     [tap setDelegate:self]; 
     [card addGestureRecognizer:tap]; 
     [tap release]; 
     //add subview 
     [container addSubview:card]; 

,并给予代表像波纹管.h文件中..

@interface ViewController : UIViewController< 
UIGestureRecognizerDelegate>{ 
//..... 
} 

我希望这会帮助你。 ..

:)

+0

我仍然得到与此相同的错误,而且我正在使用ARC。所以我不能做[点击发布]。但无论如何感谢 –

+0

好吧然后不释放水龙头,并在其工作后罚款我相信... –

0

只是更改此代码:

//set tap event with action selector = cardRowTapped 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cardRowTapped:)]; 
     [card addGestureRecognizer:tap]; 

唯一的区别是:

@selector(cardRowTapped:) 
if you have the method with parameter then ":" should be there.