Home > Software design >  Segue for UIButton Opens Same Page Regardless of which Button is Pressed
Segue for UIButton Opens Same Page Regardless of which Button is Pressed

Time:06-27

Below is my app in the simulator. It has two CollectionViews each with multiple UIButtons. I currently have a segue for UIButtons: "Good Work" and "Nice Try" in the 'firstData' string you see below, in the code.

Regardless of which one I press, however, it opens to the same view linked to identifer "Good Work." I am at a loss of how to get this to open to their own pages.

enter image description here

Code:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


}

class FirstCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var buttonOne: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        commonInit()
    }
    func commonInit() {
        guard buttonOne != nil else { return }
        buttonOne.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
        buttonOne.layer.cornerRadius = 10
        buttonOne.clipsToBounds = true
        buttonOne.layer.borderWidth = 1.0
        buttonOne.layer.borderColor = UIColor.white.cgColor
        }
    }

class SecondCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var buttonTwo: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        commonInit()
    }
    func commonInit() {
        guard buttonTwo != nil else { return }
        buttonTwo.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
        buttonTwo.layer.cornerRadius = 10
        buttonTwo.clipsToBounds = true
        buttonTwo.layer.borderWidth = 1.0
        buttonTwo.layer.borderColor = UIColor.white.cgColor
    }
}

class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    @IBOutlet weak var firstCV: UICollectionView!
    @IBOutlet weak var secondCV: UICollectionView!
    
    @IBAction func Buttons(_ sender: Any) {
        if let btn = sender as? UIButton {
            print(btn.restorationIdentifier!)
                guard let button = sender as? UIButton else { return }
                guard let id = Int(button.restorationIdentifier!) else {return}
                if id < firstData.count {
    
                 performSegue(withIdentifier: "Good Work", sender: btn)
                }
                else {
                 performSegue(withIdentifier: "Nice Try", sender: btn)
                }
                print(button.restorationIdentifier!)
               
        }
        
    }
    
    let firstData: [String] = [
        "Good Work", "Nice Try", "Btn 3", "Btn 4", "Btn 5", "Btn 6"
    ]

    let secondData: [String] = [
        "Second 1", "Second 2", "Second 3", "Second 4", "Second 5", "Second 6"
    ]

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        firstCV.dataSource = self
        firstCV.delegate = self
        
        secondCV.dataSource = self
        secondCV.delegate = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // if it's the First Collection View
        if collectionView == firstCV {
            return firstData.count
        }
        
        // it's not the First Collection View, so it's the Second one
        return secondData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if collectionView == firstCV {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
              cell.buttonOne.setTitle(firstData[indexPath.item], for: []) //allows for button title change in code above
             cell.buttonOne.restorationIdentifier = "\(indexPath.row)"
              return cell
          }

          // it's not the First Collection View, so it's the Second one
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
          cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
          cell.buttonTwo.restorationIdentifier = "\(indexPath.row   firstData.count)"
          return cell
          }
}

CodePudding user response:

The reason you only get to "Good Work" ...

In cellForItemAt you are doing this:

cell.buttonOne.restorationIdentifier = "\(indexPath.row)"

That sets the .restorationIdentifier for "Good Work" button to 0 (Zero) and the .restorationIdentifier for "Nice Try" button to 1.

Then, in @IBAction func Buttons(_ sender: Any) {:

if id < firstData.count {
    performSegue(withIdentifier: "Good Work", sender: btn)
}
else {
    performSegue(withIdentifier: "Nice Try", sender: btn)
}

Since firstData.count equals 6, and the id from the buttons will be either 0 or 1, the are, of course, each less-than-6.

Using your approach, you would likely want to do this:

if id == 0 {
    performSegue(withIdentifier: "Good Work", sender: btn)
}
else if id == 1 {
    performSegue(withIdentifier: "Nice Try", sender: btn)
}
  • Related