0

I am trying to activate a segue between a UICollectionView, subview of a UIViewController, and a separate ViewController (called RegisterController). The CollectionView is all set up programmatically, and there is a button on the last cell. However, I can't do the typical performSegue(_:withIdentfier) because the button is in the CollectionView. So, I have set up a method in the ViewController holding the CollectionView to performSegue(_:withIdentifier). I set up an instance in the CollectionViewController to call the method:

lazy var registerButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Don't have an account?", for: .normal)
    btn.setTitleColor(lighterOrange, for: .normal)
    btn.addTarget(self, action: #selector(registerScreenAppear), for: .touchUpInside)
    return btn
}()

@objc func registerScreenAppear(){
    let vc: ViewController = ViewController()
    _ = vc.toRegister()
}

The method in the ViewController is

func toRegister(){
    performSegue(withIdentifier: "toRegister", sender: self)
}

I am getting the call stack: NSInvalidArgumentException', reason: 'Receiver (<audible.ViewController: 0x7fe780724770>) has no segue with identifier 'toRegister''

I know I'm probably doing something completely wrong, but I've searched and searched and can't find a solution. Any help is appreciated, thanks

(Yes, I have configured the segue and gave it an identifier in the storyboard, but the reason I have to do this through code is because the CollectionView and the button are created programmatically, so I can't use the Interface Builder)

  • In Storyboard, press and hold key control, and drag a line from ViewController to RegisterController to create a segue named "toRegister". – Yun CHEN Jan 15 '18 at 02:00
  • @YunCHEN I have already done that – Ryan Joesting Jan 15 '18 at 02:28
  • You are creating a new instance of your view controller, not referring to the existing instance. This new instance isn't linked to the storyboard since you have used a simple initialiser to create it. You need to use delegation or a closure. See https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 for some ideas on how to get an event from a cell back to a view controller – Paulw11 Jan 15 '18 at 03:03

1 Answers1

1

call performSegue in registerScreenAppear method or call self.toRegister() like

@objc func registerScreenAppear(){
    self.toRegister()
}

OR

 @objc func registerScreenAppear(){
    performSegue(withIdentifier: "toRegister", sender: self)
 }

Note: use later one as it will remove the extra function call i.e. toRegister().

Van
  • 1,225
  • 10
  • 18
  • Because the registerScreenAppear method is in a UICollectionViewCell class, perfromSegue(_:withIdentifier) will give an 'Unresolved use of identifier' error. – Ryan Joesting Jan 15 '18 at 13:50
  • Okay got your problem, you can move the addTarget line: " btn.addTarget(self, action: #selector(registerScreenAppear), for: .touchUpInside) " to view controller and add it in cellForItem method while configuring cell. and also move the respective target method to view controller. – Van Jan 16 '18 at 05:37