Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Segue from Button inside a cell

Tags:

swift

cell

segue

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:

Screenshot

like image 747
Abdou023 Avatar asked Oct 01 '14 14:10

Abdou023


People also ask

How to create a segue from a uitableviewcell to another viewcontroller?

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.

How do I add a segue to an existing view controller?

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.

How to create a UIViewController in Swift?

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”.

How to set the label of a view controller in Swift?

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”.


2 Answers

-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)
}
like image 190
mustafa Avatar answered Nov 16 '22 03:11

mustafa


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.

like image 42
zisoft Avatar answered Nov 16 '22 04:11

zisoft