简体   繁体   中英

possible to segue with xib gesture recognizer?

I am trying to go to a different segue if the user clicks on an element like imageview from the xib (via gesture recognizer) instead of the rest of the row in a tableView (since that is a different segue). I have seen people do it with objective C, but I am not sure how to do it in Swift, has anyone done it before successfully? I know I would have to make the code changes on the custom xib file and then get the actual controller to make the transition (if I am to go by the objective C way).

So if you have a UIButton in your cell, you can use the below code to performSegue on its click.

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : YourCell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath) as! YourCell
    cell.imageButton.addTarget(self, action: #selector(imageTapped(sender:)), for: .touchUpInside)
    return cell
}


@objc func imageTapped(sender : UIButton) {
    self.performSegue(withIdentifier: "YourIdentifer", sender: self)
}

So in cellForRowAt, we are assigning your imageButton a function that will be called whenever the button is clicked ie imageTapped()

As your imageTapped function is in your viewController, you can easily perform a segue by creating one from VC1 to VC2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM