mercredi 12 février 2020

Swift- Check which button is selected in different class using if-statement

I have 4 different buttons that lead to the same view controller SearchPage(), thus I assigned these buttons in the main yearOne class like this:

class yearOne: UICollectionViewController:

    @objc func buttonAction(sender: UIButton!) {
        var tagVal : NSInteger = sender.tag
        switch sender.tag {
        case 0:
            print("ONEEE")
            let destination = SearchPage()
            navigationController?.pushViewController(destination, animated: true)
        case 1:
            print("TWOOO")
            let destination = SearchPage()
            navigationController?.pushViewController(destination, animated: true)
        case 2:
            print("THREEE")
            let destination = SearchPage()
            navigationController?.pushViewController(destination, animated: true)
        case 3:
            print("FOURRR")
            let destination = SearchPage()
            navigationController?.pushViewController(destination, animated: true)
        default:
            let destination = SearchPage()
            navigationController?.pushViewController(destination, animated: true)
        }

    }

each of the buttons is in different classes

class fallQuarterCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource:

    let addButton : UIButton = {
        let button = UIButton()//frame: CGRect(x: 345, y: 10, width: 30, height: 30))
        button.setImage(UIImage(named: "addicon"), for: .normal)
        button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        button.tag = 0
        button.addTarget(self, action: #selector(yearOne.buttonAction), for: .touchUpInside)
        return button
    }()

class winterQuarterCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource:

    let addButton : UIButton = {
        let button = UIButton()//frame: CGRect(x: 345, y: 10, width: 30, height: 30))
        button.setImage(UIImage(named: "addicon"), for: .normal)
        button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        button.tag = 1
        button.addTarget(self, action: #selector(yearOne.buttonAction), for: .touchUpInside)
        return button
    }()

and so on with the third and fourth buttons. Anyways the button does work and each one prints out correctly and sends me to the SearchPage()

However in my SearchPage() class, I'm trying to recall the sender.tag?? so that I can use if/else statement on which button was pressed/selected.

    let mainvc = yearOne()
    let vc = fallQuarterCell()
    let vc2 = winterQuarterCell()
    let vc3 = springQuarterCell()
    let vc4 = summerQuarterCell()

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

        tableView.deselectRow(at: indexPath, animated: true)

        if (mainvc.tagValue == 0){
            if (resultSearchController.isActive){

                vc.data.append(filteredCourses[indexPath.row])
                UserDefaults.standard.set(vc.data, forKey: "SavedArray")

                navigationController?.popViewController(animated: true)

            }
            else{
                vc.data.append(allCourses[indexPath.row] as! String)
                UserDefaults.standard.set(vc.data, forKey: "SavedArray")

                navigationController?.popViewController(animated: true)

            }
        }
        else if (mainvc.tagValue == 1){
            if (resultSearchController.isActive){
                vc2.data.append(filteredCourses[indexPath.row])
                UserDefaults.standard.set(vc2.data, forKey: "SavedArray2")
                vc2.tableView.reloadData()
                navigationController?.popViewController(animated: true)
            }
            else{
                vc2.data.append(allCourses[indexPath.row] as! String)
                UserDefaults.standard.set(vc2.data, forKey: "SavedArray2")
                vc2.tableView.reloadData()
                navigationController?.popViewController(animated: true)
            }
        }

    }

Does anyone have any idea on how I can approach this? Right now because I added the tagVal inside the button func, the yearOne aka mainvc doesn't seem to have a member.

Aucun commentaire:

Enregistrer un commentaire