ios - 如何知道从另一个 View Controller 中的 UICollectionViewController 按下了哪个按钮

标签 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)
        }
}

每当单击按钮时, View Controller 都会导航到另一个 View Controller 代码

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

但是点击任何按钮都会导航到同一个 View Controller 因为我使用段 View Controller ,并且对于特定的索引,将显示不同的 View ,所以我必须检查从 Collection View 中选择了哪个按钮 我使用标签进行检查

cell.btn.tag = indexPath.row

但它不起作用 我无法访问标签

简而言之,我的问题是如何检查在推送到另一个 View Controller 时单击了哪个按钮(来自 collectionview) 提前致谢

最佳答案

也许这对你有帮助:

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)
}

关于ios - 如何知道从另一个 View Controller 中的 UICollectionViewController 按下了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067252/

相关文章:

swift - 为什么 NSTextField 只有在我点击字符串后才变得可点击?

ios - 以编程方式更改 UICollectionView 中的第一项

ios - 如何在已经是 NIB 的 UICollectionViewCell 中加载带有自定义单元格的 UITable

ios - 按下按钮时更改为不同的 VIewController

ios - 带有 UIButton : unable to change UIImage 的自定义 UITableViewCell

ios - 将数据从 ViewController 传递到 Representable SwiftUI

ios - 有什么方法可以快速绘制和分析数据吗?

ios - URL AFNetworking GET 中的变音符号

ios - 如何在 Swift 4 中更新 Firebase 身份验证中的电子邮件地址

ios - UICollectionView registerClass : forCellWithReuseIdentifier method breaks UICollectionView