Ios Swift-IBACTION如何执行segue
我可以使用Ios Swift-IBACTION如何执行segue,ios,swift,tableview,segue,Ios,Swift,Tableview,Segue,我可以使用didselectrowatinexpath成功地将数据(authorID)传递给视图。 现在,我尝试用按钮iAction执行相同的步骤,但找不到执行的方法。我找不到在不活动中使用nsindepath的方法 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { performSegueWithIdentifier("Article
didselectrowatinexpath
成功地将数据(authorID)传递给视图。
现在,我尝试用按钮iAction执行相同的步骤,但找不到执行的方法。我找不到在不活动中使用nsindepath的方法
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) {
//???
}
如果已将segue定义为两个视图控制器之间的手动segue,则只需调用
performSegueWithIdentifier("ManualSegue", sender: self)
另一种可能是定义从按钮本身到目标视图控制器的顺序。在这种情况下,它会自动发生;您既不需要
iAction
,也不需要调用performsguewithidentifier
有一个方法
performSegueWithIdentifier("ArticleSegue", sender:self)
将
行
分配给用户界面中ui按钮的标记
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Setup cell etc..
....
cell.favButton.tag = indexPath.row
...
return cell
}
然后在动作方法中,注意方法从AnyObject
到ui按钮的变化代码>以便您可以访问标记属性
@IBAction func favButtonDidTouch(sender: UIButton!) {
performSegueWithIdentifier("ArticleSegue", sender:NSIndexPath(forRow: sender.tag, inSection: 0))
}
解决这个问题的一种方法是,您只需要单元格的行(这里是articleList的indexath.row),即为favButtons提供一个标记(如果您不将UIView标记用于其他内容)
这意味着当您在tableView:cellforrowatinexpath:
中配置单元格时,只需说:
cell.favButton.tag = indexPath.row
然后在IBAction中,使用该标记创建NSIndexPath并调用segue:
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
self.performSegueWithIdentifier("ManualSegue", sender: indexPath)
确保本例中的发送方是UIView(此处为UIButton)。那么tableView中的每一行都有一个favButton?你使用自定义单元格吗?是的,我有一个自定义单元格,每一行都有一个favButtonI不能这样做,因为每一行都有一个uniq id,我需要通过segue传递我将如何在prepareforsgue中调用它?在if检查中,我发现“让indepath=sender as!nsindepath”错误。错误是“无法将“UIButton”(0x111798f68)类型的值强制转换为“NSIndexPath”(0x10f670680),然后您可能通过脚本中的favButton配置了一个segue。如果您单击故事板中的序列,请检查favButton是否高亮显示。如果是这样,请删除segue并通过单击“发送”viewcontroller创建一个新的手动segue,并将控件拖动到segue的目标viewcontroller。我需要如何在prepareForSegue中调用“indexPath”?您的调用与之前相同,当您传递的nsindepath
对象与您在发布的错误中传递的didselectrowatinexpath
对象相同时,您传递的ui按钮似乎不正确。