2015-09-28 186 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];