简体   繁体   中英

How can I call segue from uitableviewcell class? Not controller

Not controller. I want to call segue from the customcell.swift class

Also preparefor segue method does not show

It happens because you're not into in the view controller more, and you don't have access to navigation stack methods...

A great way to solve this, you can just use delegate pattern using protocol.

So you can create a protocol with an action as the code below:

protocol CustomCellDelegate {
   func didTapAnyButton(_ anyParam: String)
}

Into your UITableViewCell, you just create this delegate property:

weak var delegate: CustomCellDelegate?

Ps: I've created with weak to avoid memory leaks, and the optional, because the property will initialized with nil value, and the viewController will set the value to this property after it was already created.

So, in your ViewController you can just implement this protocol:

extension ViewController: CustomCellDelegate {
   func didTapAnyButton(_ anyParam: String) {
       performSegue()
   }
}

At least you should just assign the ViewController delegate when you instantiate the CustomCell, considering you have an UITableViewCell called CustomCell:

let cell = tableView.dequeReusableCellWithIdentifier(“CustomCel”) as! CustomCell

cell.delegate = self

After that, you can just call the delegate method tapAnyButton at the action you need to implement, for example:

delegate.didTapAnyButton(“Some Param that I need to pass on segue method.”)

So when you call this method, the ViewController that assigned this delegate protocol will be trigged! And in the ViewController you have access to the segue method.

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