如何知道从另一个视图控制器中的UICollectionViewController按下了哪个按钮

时间:2015-08-18 08:24:37

标签: ios swift uicollectionview

我正在创建一个使用3个按钮网格的应用程序。 我使用了UICollectionViewController并向UICollectionViewCell添加了按钮 还为每个按钮单击事件添加目标 下面的代码

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提前

3 个答案:

答案 0 :(得分: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)
}

答案 1 :(得分:0)

尝试为destinationViewController编写属性。 例如showIndex

然后你可以修改你的第三个功能:

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

不清楚我是否清楚地理解你的问题。

答案 2 :(得分: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)

}