我正在創建一個應用程序,其中使用了3個按鈕網格。 我用UICollectionViewController該按鈕並將其添加到UICollectionViewCell 同樣對於每個按鈕點擊事件 添加低於目標代碼如何知道從另一個視圖控制器中的UICollectionViewController按下哪個按鈕
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
if indexPath.row == 0 {
cell.btn.addTarget(self, action: "first", forControlEvents: UIControlEvents.TouchUpInside)
cell.btn.setTitle("1st", forState: .Normal)
}else if indexPath.row == 1 {
cell.btn.addTarget(self, action: "second", forControlEvents: UIControlEvents.TouchUpInside)
cell.btn.setTitle("2nd", forState: .Normal)
}else if indexPath.row == 2 {
cell.btn.addTarget(self, action: "third", forControlEvents: UIControlEvents.TouchUpInside)
cell.btn.setTitle("3rd", forState: .Normal)
}
}
每當按鈕被點擊視圖控制器導航到另一個視圖控制器 碼作爲
var destinationViewController : UIViewController!
func third(){
destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
navigationController?.pushViewController(destinationViewController, animated: true)
}
但導航到任何按鈕點擊相同的視圖控制器 ,因爲我使用段視圖控制器和特定索引不同的視圖將顯示,所以我必須檢查哪個對接對從集合視圖 選擇我用標籤檢查
cell.btn.tag = indexPath.row
,但它不工作 我無法訪問標籤
總之,我的問題是,如何檢查被點擊了哪個按鈕(從的CollectionView)時推到另一個視圖控制器 Thanx提前
您的功能(第一,第二,第三)應具有參數 「發件人」。這樣發送者將是被點擊的按鈕。 – Evgeniy
爲什麼不能訪問標籤? – Bannings
應用程序崩潰當我訪問標籤使用withViewTag() –