Gun*_*tel 1 ios uicollectionview swift
我正在创建一个使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
只要单击按钮,视图控制器就会导航到另一个视图控制器代码
var destinationViewController : UIViewController!
func third(){
destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
navigationController?.pushViewController(destinationViewController, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
但导航到同一视图控制器的任何按钮点击,因为我使用段视图控制器和特定索引不同视图将显示所以我必须检查从集合视图中选择哪个按钮我用标签检查
cell.btn.tag = indexPath.row
Run Code Online (Sandbox Code Playgroud)
但它无法正常工作我无法访问标签
简而言之,我的问题是如何在提前推送到另一个视图控制器Thanx时检查哪个按钮(来自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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3361 次 |
| 最近记录: |