2015-09-28 188 views
0

林具有增加tapRecognizers我的意见的一些问题。 即时通讯添加未知数量的事件,新闻和优惠券到我的UIScrollView点击我想打开一个详细视图。然而在点击应用程序崩溃与以下错误添加UITapGestureRecognizer动态增加的视图

Almhults_appen.MainActivity redirectFromHomeScreen:]: unrecognized selector sent to instance 0x7f93c2d4df20 

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Almhults_appen.MainActivity redirectFromHomeScreen:]: 

当我看到它,我可以让使用的UITableView和所有视图添加到它。但是,如果可能的话,我个人会喜欢避免它,但因为我是Swift的新手,所以我不相信我在这里的判断。

if (!event_ar.isEmpty) { 
    for event: Event in event_ar { 

     ... Init EventView and add to UIScrollView 

     // Add tapGesture 
     let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(
      target: self, 
      action: "redirectFromHomeScreen:" 
     ) 
     eventView.addGestureRecognizer(tapGesture) 
    } 
} 
if (!news_ar.isEmpty) { 
    ... Add news identically to events 
} 
if (!coupon_ar.isEmpty) { 
    ... Add coupons identically to events 
} 

编辑提前:)

回答

0

根据您的崩溃报告。首先需要声明funtion

func redirectFromHomeScreen (sender : UITapGestureRecognizer){ 

} 

申报并再次尝试

+0

添加了redirectFromHomeScreen()来提问。 截至目前它被声明为private是一些可能引起麻烦 –

+0

愚蠢的我由于我曾发生redirectFromHomeScreen问题()被声明为private。 非常感谢:) –

0

你有你的手势添加到你会点按视图中添加动作功能

private func redirectFromHomeScreen(sender: UITapGestureRecognizer) -> Void{ 
    ... Do stuff 
} 

感谢。添加它的最佳时机是在该视图的viewDidLoad方法中。通过Objective-C,我可以做到这一点,并且效果很好。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanToFlipPage:)]; 
    [self.view addGestureRecognizer:pan]; 

}

+0

截至目前,我在viewDidLoad中调用一个单独的函数内添加views和tapGestures。正如我所看到的将scrollview的填充移动到viewDidLoad不会产生影响。 –

0

尝试添加这些行已创建后所有的子视图和按钮。

[self.view setNeedsLayout]; 
[self.view layoutIfNeeded];