How to pass parameter in button's addtarget

52 Views Asked by At

I have a bottom sheet, and on that sheet there is a button. I want that when i tap to that button it go to the page it is related to. So, i connected that from storyboard to code

@IBOutlet weak var openPage: SheetOpenPageButton!

And wrote extension for the button.

class SheetOpenPageButton: UIButton {
    var navigationController: UINavigationController?
    override var isSelected: Bool {
        didSet {
            if isSelected {
               self.backgroundColor = UIColor(named: "CustomRed")
            } else {
                self.backgroundColor = UIColor(named: "CustomGray")
            }
        }
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

    }
    
    required override init(frame: CGRect) {
        super.init(frame: frame)
    }
}

In the collectionview, where i am clicking on button, i call these methods.

let button = SheetOpenPageButton()
                button.tag = cell.id ?? 1
                button.addTarget(self, action: #selector(goToRestuarantFromStory(_ :)), for: .touchUpInside)

  @objc func goToRestuarantFromStory(_ sender : UIButton) {
        let restuarantVC = UIStoryboard(name: "Cafe", bundle: .main).instantiateViewController(withIdentifier: "RestuarantViewController") as! RestuarantViewController
        restuarantVC.networkId = sender.tag
        navigationController?.pushViewController(restuarantVC, animated: true)
        print("smth")
        removeSubview()
        removeSubviewSpb()
        vc.dismiss(animated: true, completion: nil)
    }

Here i am passing parameter in addtarget using button's tag. But, when tapping on button it doesn't give a reaction. I don't know how to solve this.

1

There are 1 best solutions below

1
DatBlaueHus On

SheetOpenPageButton does not need a navigationController, it's neither used nor instantiated in the code you have shown here.

The navigationContoller your refer to in goToRestuarantFromStory [sic] is from the scope of your collectionView and according to your code also nil, so pushViewController is never actually called.