Search code examples
iosswiftsegueuialertaction

Swift UIAlertAction segue data pass


I am using a UIAlertAction to ask the user if they want to go to that users profile. Not sure what I am doing wrong because the data doesn't pass correctly and returns nil. Not sure If I need to have the "prepareForSegue" outside the UIAlertAction... I guess the segue passes before the data with this current setup?

let profileAction = UIAlertAction(title: "Go To Profile", style: UIAlertActionStyle.default, handler: { action in self.performSegue(withIdentifier: "followingfeed", sender: self)

        let dataPass = self.feeds[sender.tag].dataPass

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            super.prepare(for: segue, sender: sender)
            if segue.identifier == "followingfeed" {

                    let user = dataPass
                    let controller = segue.destination as? ExploreBusinessProfileSwitchView
                    controller?.otherUser = user

            }
        }
    })

Solution

  • prepareForSegue should be a class's instance method

    let profileAction = UIAlertAction(title: "Go To Profile", style: UIAlertActionStyle.default, handler: { action in 
      self.performSegue(withIdentifier: "followingfeed", sender:self.feeds[sender.tag].dataPass)
    })
    

    //

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       super.prepare(for: segue, sender: sender)
         if segue.identifier == "followingfeed" {
             let user = sender as! [String:Any]
             let controller = segue.destination as? ExploreBusinessProfileSwitchView
             controller?.otherUser = user 
         }
    }