Swift UIAlertAction segue数据传递

时间:2018-09-28 00:47:42

标签: ios swift segue uialertaction

我正在使用UIAlertAction询问用户是否要转到该用户个人资料。不知道我在做什么错,因为数据无法正确传递并返回nil。不确定是否需要在UIAlertAction之外使用“ prepareForSegue” ...我猜想segue在使用当前设置的数据之前通过了?

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

            }
        }
    })

1 个答案:

答案 0 :(得分:1)

prepareForSegue应该是类的实例方法

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 
     }
}