问题
i have a custom cell attached to TableViewCell class, and i have a button inside that custom cell, i want whey i press the button it segues to another view controller, but the:
performSegueWithIdentifier(identifier: String, sender: AnyObject?)
function is not recognised, how to fix that ?
Edit:

回答1:
-performSegueWithIdentifier:
method is declared in UIViewController
. So you can't just call it in UITableViewCell
subclass.
You can add an action to that button when you are creating cell in -tableView:cellForRowAtIndexpath:
method. Then you can call -performSegueWithIdentifier:
method in that action method. Here is example assuming we are in UITableViewController
subclass:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.button.addTarget(self, action: "someAction", forControlEvents: .TouchUpInside)
return cell
}
And here is action method:
func someAction() {
self.performSegueWithIdentifier("moveToView", sender: self)
}
回答2:
You need a custom class for your cell. In that class, create an @IBAction
as response to the button click. In that action, perform your segue.
回答3:
If you are using Interface Builder:
Connect your firstViewController to the secondViewController - and create a Segue Identifier.
Then you are able to use
performSegueWithIdentifier(identifier: String, sender: AnyObject?)
来源:https://stackoverflow.com/questions/26143820/swift-segue-from-button-inside-a-cell