0

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.

  • You say "where I am clicking on button..." - But presumably the first line, where you create the button isn't in your tap handler? Wher is that code? You have your button as an IBOutlet - Where is that outlet? In your collection view cell? Have you connected that outlet to a button in your storyboard? If so, set the touchUpInside handler there. You shouldn't create a random instance of a button that isn't in your view anywhere – Paulw11 Jul 02 '22 at 22:45
  • It doesn't push the vc, tag passes correctly to the pushed vc - @Sh_Khan – Mariam Sargsyan Jul 03 '22 at 11:13
  • When I try to use in this way- IBOutlet weak var openPage: UIButton!(Without custom button), when i want to call that button from collection view it returns me button as nil. I don't know how to pass button that it not become nil. - @Paulw11 – Mariam Sargsyan Jul 03 '22 at 11:17
  • When I try to use in this way- IBOutlet weak var openPage: UIButton!(Without custom button), when i want to call that button from collection view it returns me button as nil. I don't know how to pass button that it not become nil. @Sh_Khan – Mariam Sargsyan Jul 03 '22 at 11:18
  • It is connected, also i tried with ibaction but it behalved the same way. Maybe i am passing button wrong way? Is it true to call button, declaring let vc = SheetViewController() and calling like this- vc.button? Maybe there is another way to call button, so that dont come nil? - @Sh_Khan – Mariam Sargsyan Jul 03 '22 at 11:44
  • Unfortunately i can't, because it is a private project( - @Sh_Khan – Mariam Sargsyan Jul 03 '22 at 11:46
  • @MariamSargsyan - start simple... create a new project and work on your collection view / cell / buttons. Are you using Storyboard with a `UICollectionViewController` and cell prototype? Or are you doing everything via code? – DonMag Jul 03 '22 at 13:51
  • i am using both code and storyboard - @DonMag – Mariam Sargsyan Jul 03 '22 at 17:30
  • Dears, thanks for your answers, i found a way to do this. I am just creating button with code and then calling it. In ths case it is not nil and everything works well. So, when i tried to call from ibOutlet it was coming nil because it wasn't initialising in viewdidload. – Mariam Sargsyan Jul 03 '22 at 17:32

1 Answers1

0

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.

DatBlaueHus
  • 266
  • 1
  • 5
  • Thanks for your answer. The problem was that when calling button it was coming as nil. I found the solution and described it in the first answer's comment. – Mariam Sargsyan Jul 03 '22 at 17:34