【发布时间】: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