ios - Swift - IBACTION 如何执行 segue

标签 ios swift tableview segue

我可以使用 DidSelectRowAtIndexPath 成功地将数据 (authorID) 传递给 View 。 现在我尝试使用 Button IBACTION 执行相同的 segue,但我找不到执行它的方法。我找不到在 INACTION 中使用 NSIndexPath 的方法。

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       performSegueWithIdentifier("ArticleSegue", sender: indexPath) 
       tableView.deselectRowAtIndexPath(indexPath, animated: true)
 }

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ArticleSegue"{
            let toView = segue.destinationViewController as! ArticleTableViewController
            let indexPath = sender as! NSIndexPath
            let authorid = articleList?[indexPath.row]["author_id"].int!
            toView.authorid = authorid            
}

@IBAction func favButtonDidTouch(sender: AnyObject) {
 //???
}

最佳答案

您可以解决它的一种方法,因为您只需要单元格的行(这里是您的文章列表的 indexPath.row),就是给您的 favButtons 一个标签(如果您不使用 UIView-tags 做其他事情).

这意味着当您在 tableView:cellForRowAtIndexPath: 中配置您的单元格时,您只需说:

cell.favButton.tag = indexPath.row

然后在您的 IBAction 中,使用该标记创建一个 NSIndexPath 并调用 segue:

let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
self.performSegueWithIdentifier("ManualSegue", sender: indexPath)

确保本例中的发送者是一个 UIView(这里是 UIButton)。

关于ios - Swift - IBACTION 如何执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31379138/

相关文章:

ios - NSUserDefaults 返回 NSDictionary 而不是 NSArray

iOS:如何像 iBook 一样将图像放在 tableView 的顶部

swift - 在 Swift 字典中重置值时崩溃

ios - NSFileManager 创建文件失败

ios - 多对一

ios - 如何使用 Swift 制作项目符号列表?

ios - 喜欢 Facebook 页面上的帖子

ios - 设置 UISwitch 的初始状态

ios - 快速 editActionsForRowAtIndexPath

qt - 使用 QList<QStringList> 填充 QTableView。当 View 中的某些内容发生更改时,如何将其恢复到数据中?