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:
To create a segue from a UITableViewCell to another View controller, we’ll do it like any other ViewController to ViewController segue. We’ll do this with help of an example here. First Create a project, delete the View Controller from storyboard and add One Table View Controller and one View Controller in the storyboard.
Select that segue, and in the Attributes Inspector, give it an identifier with the name “ShowBlogSegue”: We’re going to need a custom class for our new view controller, so create one from the top menu via File → New → File…, in which we will create a “Cocoa Touch Class” in the iOS → Source section.
For that, we need to create a new Swift file to make this View Controller that type. So, go to “File → New → File…”, and and in the “iOS → Source” section, select “Cocoa Touch Class”. Then in there call this Class “OtherViewController”, and make sure to set the “Subclass of” to “UIViewController”.
If we want to be able to set that label, we need to be able to make an outlet to it in code. For that, we need to create a new Swift file to make this View Controller that type. So, go to “File → New → File…”, and and in the “iOS → Source” section, select “Cocoa Touch Class”.
-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)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With