简体   繁体   English

Swift-IBACTION如何执行搜寻

[英]Swift - How an IBACTION performs segue

I can pass data (authorID) to a view using DidSelectRowAtIndexPath successfully. 我可以使用DidSelectRowAtIndexPath成功地将数据(authorID)传递到视图。 Now I try to perform the same segue with an Button IBACTION, But I couldn't find a way to perform it. 现在,我尝试使用Button IBACTION执行相同的segue,但是我找不到执行它的方法。 I couldn't find a way to use NSIndexPath inside INACTION. 我找不到在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) {
 //???
}

If you have defined the segue as a manual segue between two view controllers, you can just call 如果您已将segue定义为两个视图控制器之间的手动segue,则只需调用

performSegueWithIdentifier("ManualSegue", sender: self)

Another possibility is that you define the segue from the button itself to the target view controller. 另一种可能性是您定义从按钮本身到目标视图控制器的序列。 In that case, it happens automatically; 在这种情况下,它会自动发生; you need neither an IBAction nor a call to performSegueWithIdentifier . 您既不需要IBAction也不需要调用performSegueWithIdentifier

One way you could solve it, as you just need the row of the cell (here indexPath.row for your articleList), is to give your favButtons a tag (if you don't use the UIView-tags for something else). 解决问题的一种方法是,仅需要单元格的行(此处为articleList的indexPath.row),就是为favButtons提供一个标签(如果您不将UIView-tag用于其他用途)。

This means when you configure your cell in tableView:cellForRowAtIndexPath: you just say: 这意味着当您在tableView:cellForRowAtIndexPath:配置单元格时,您只会说:

cell.favButton.tag = indexPath.row

then in your IBAction, create an NSIndexPath with that tag and call the segue: 然后在您的IBAction中,使用该标签创建一个NSIndexPath并调用segue:

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

Make sure the sender in this case is an UIView (here UIButton). 在这种情况下,请确保发送者是UIView(此处为UIButton)。

有一种方法

performSegueWithIdentifier("ArticleSegue", sender:self)

Assign the Row to the tag of the UIButton in your Row分配给您的UIButton的标签

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // Setup cell etc..
    ....
    cell.favButton.tag = indexPath.row
    ...
    return cell
}

Then in your action method, note the change in the method from AnyObject to UIButton! 然后在您的操作方法中,注意方法从AnyObjectUIButton! so you can access the tag property. 因此您可以访问标签属性。

@IBAction func favButtonDidTouch(sender: UIButton!) {
  performSegueWithIdentifier("ArticleSegue", sender:NSIndexPath(forRow: sender.tag, inSection: 0))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM