Reputation: 289
I am creating one application in which 3 button grid used. I used UICollectionViewController for that and add buttons to UICollectionViewCell Also add target for each button click event below code
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)
}
}
Whenever button is clicked view controller navigate to another view controller code for that
var destinationViewController : UIViewController!
func third(){
destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
navigationController?.pushViewController(destinationViewController, animated: true)
}
but navigation to same view controller for any button clicked because I use segment view controller and for particular index different view will displayed so I have to check which button is selected from collection view I used tag for checking
cell.btn.tag = indexPath.row
but its not working I can not access tag
In short my question is how to check which button(from collectionview)is clicked when push to another view controller Thanx in advance
Upvotes: 1
Views: 3872
Reputation: 10479
Maybe this will help you:
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)
}
Upvotes: 6
Reputation: 4371
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)
}
}
for target user this
func third(sender:UIButton?){
var tag:Int = sender.tag
destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
navigationController?.pushViewController(destinationViewController, animated: true)
}
Upvotes: 0
Reputation: 796
Try to write a property for your destinationViewController. e.g. showIndex
Then you can modify your func third:
func third(){
destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("AddInventory") as! UIViewController
destinationViewController.showIndex = 3;
navigationController?.pushViewController(destinationViewController, animated: true)
}
Don't know if I understand your question clearly.
Upvotes: 0