【问题标题】:How to know which button is pressed from UICollectionViewController in another view controller如何知道从另一个视图控制器中的 UICollectionViewController 按下了哪个按钮
【发布时间】:2015-08-18 08:24:36
【问题描述】:

我正在创建一个使用 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) 提前感谢

【问题讨论】:

  • 你的函数(第一,第二,第三)应该有参数“sender”。这样,发送者将成为被点击的按钮。
  • 为什么无法访问标签?
  • 当我使用 withViewTag() 访问标签时应用程序崩溃
  • @GunjaPatel 发布关于使用viewWithTag的代码
  • 我正在尝试在目标视图控制器 viewdidload 方法中添加用于访问标签的代码,例如 var home = HomeCollectionViewController() home.collectionView?.viewWithTag(7) HomeCollectionViewController 是我在其中使用集合的类

标签: ios swift uicollectionview


【解决方案1】:

也许这会对你有所帮助:

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

【讨论】:

  • 我像你的回答一样修改了我的函数,但它给出了 UIViewController 没有名为“fromTag”的成员的错误,你能告诉我你使用的“fromTag”的含义吗
  • 它将在destinationViewController 中声明,然后您将知道在destinationViewController 中按下了哪个按钮。您必须创建一个自定义 viewController(UIViewController 的子类)。
  • 它的工作!只改变将destinationviewcontroller 转换为视图控制器的一件事。在我的情况下 DestinationViewController
【解决方案2】:

尝试为您的destinationViewController 编写一个属性。 例如显示索引

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

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

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

【讨论】:

  • 无法理解您的问题
【解决方案3】:
    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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2021-10-08
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多