Swift - Segue from Button inside a cell

寵の児 提交于 2019-12-04 21:00:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!