2015-08-18 18 views
0

我正在創建一個應用程序,其中使用了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提前

+0

您的功能(第一,第二,第三)應具有參數 「發件人」。這樣發送者將是被點擊的按鈕。 – Evgeniy

+0

爲什麼不能訪問標籤? – Bannings

+0

應用程序崩潰當我訪問標籤使用withViewTag() –

回答

5

也許這將幫助你:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell : CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

    cell.btn.tag = indexPath.row 
    cell.btn.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside) 
} 

func buttonClicked(sender: UIButton?) { 
    let tag = sender.tag 

    let destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! DestinationViewController 
    destinationViewController.fromTag = tag 
    navigationController?.pushViewController(destinationViewController, animated: true) 
} 
+0

我修改我的func就像你的答案,但它給錯誤,UIViewController沒有名爲'fromTag'的成員可以請告訴我「FromTag」的意思,你使用 –

+0

它將在' destinationViewController'然後你就會知道'destinationViewController'中按下了哪個按鈕。你必須創建一個自定義的viewController(UIViewController的子類)。 – Bannings

+0

其工作!只改變目標控制器作爲視圖控制器投射的一件事。在我的情況下DestinationViewController –

0

嘗試爲您的destinationViewController編寫一個屬性。 例如showIndex

然後你就可以修改你的FUNC第三:

func third(){ 
    destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController 
    destinationViewController.showIndex = 3; 
    navigationController?.pushViewController(destinationViewController, animated: true) 
} 

不知道我理解你的問題清楚。

+0

不能理解你的問題 –

0
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.tag = indexpath.row 
       cell.btn.setTitle("1st", forState: .Normal) 
      }else if indexPath.row == 1 { 
       cell.btn.addTarget(self, action: "second:", forControlEvents: UIControlEvents.TouchUpInside) 
cell.tag = indexpath.row 
       cell.btn.setTitle("2nd", forState: .Normal) 
      }else if indexPath.row == 2 {    
       cell.btn.addTarget(self, action: "third:", forControlEvents: UIControlEvents.TouchUpInside) 
cell.tag = indexpath.row 
       cell.btn.setTitle("3rd", forState: .Normal) 
      } 
    } 

爲目標用戶該

func third(sender:UIButton?){ 
    var tag:Int = sender.tag 
    destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController 
    navigationController?.pushViewController(destinationViewController, animated: true) 

} 
相關問題