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

Ios 如何知道在另一个视图控制器的UICollectionViewController中按下了哪个按钮,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在创建一个应用程序,其中使用了3个按钮网格。 我使用UICollectionViewController实现了这一点,并向UICollectionViewCell添加了按钮 还为每个按钮单击事件添加目标 以下代码 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

我正在创建一个应用程序,其中使用了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提前

尝试为destinationViewController编写属性。 e、 g.展示指数

然后,您可以修改第三个函数:

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

不知道我是否清楚地理解了您的问题。

尝试为您的destinationViewController编写一个属性。
    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)
            }
    }
e、 g.展示指数

然后,您可以修改第三个函数:

func third(){
   destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
   destinationViewController.showIndex = 3;
   navigationController?.pushViewController(destinationViewController, animated: true)
}
不知道我是否明白你的问题

    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)

}
对于目标用户,此

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

}

也许这会帮助你:

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

也许这会帮助你:

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

无法理解您的问题无法理解您的问题您的函数(第一、第二、第三)应具有参数“sender”。这样,发送者将是被点击的按钮。为什么不能访问标签?当我使用withViewTag()访问标签时,应用程序定位崩溃@GunjaPatel发布关于使用
viewWithTag
的代码我正在尝试添加代码,以便在目标视图控制器viewdidload方法中访问标记,如var home=HomeCollectionViewController()home.collectionView?.viewWithTag(7)HomeCollectionViewController是我使用collectionYou函数(第一、第二、第三)的类,应该有参数“sender”。这样,发送者将是被点击的按钮。为什么不能访问标签?当我使用withViewTag()访问标签时,应用程序定位崩溃@GunjaPatel发布关于使用
viewWithTag
的代码我正在尝试添加代码,以便在目标视图控制器viewdidload方法中访问标记,如var home=HomeCollectionViewController()home.collectionView?.viewWithTag(7)HomeCollectionViewController是我的类,我在其中使用collectionI修改我的func,就像您的答案一样,但它给出了一个错误,UIViewController没有名为“fromTag”的成员,您能告诉我“fromTag”的含义吗您使用的是哪个将在
destinationViewController
中声明,然后您将知道在
destinationViewController
中按下了哪个按钮。您必须创建一个自定义viewController(UIViewController的子类
UIViewController
)!!!仅更改一件事,即将destinationviewcontroller转换为视图控制器。在我的例子中,DestinationViewControllerI按照您的答案修改了我的函数,但它给出了一个错误,即UIViewController没有名为“fromTag”的成员。您能告诉我“fromTag”的含义吗您使用的是哪个将在
destinationViewController
中声明,然后您将知道在
destinationViewController
中按下了哪个按钮。您必须创建一个自定义viewController(UIViewController的子类
UIViewController
)!!!仅更改一件事,即将destinationviewcontroller转换为视图控制器。就我而言,DestinationViewController