[英]Swift - How an IBACTION performs segue
我可以使用DidSelectRowAtIndexPath
成功地將數據(authorID)傳遞到視圖。 現在,我嘗試使用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) {
//???
}
如果您已將segue定義為兩個視圖控制器之間的手動segue,則只需調用
performSegueWithIdentifier("ManualSegue", sender: self)
另一種可能性是您定義從按鈕本身到目標視圖控制器的序列。 在這種情況下,它會自動發生; 您既不需要IBAction
也不需要調用performSegueWithIdentifier
。
解決問題的一種方法是,僅需要單元格的行(此處為articleList的indexPath.row),就是為favButtons提供一個標簽(如果您不將UIView-tag用於其他用途)。
這意味着當您在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)。
有一種方法
performSegueWithIdentifier("ArticleSegue", sender:self)
將Row
分配給您的UIButton
的標簽
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Setup cell etc..
....
cell.favButton.tag = indexPath.row
...
return cell
}
然后在您的操作方法中,注意方法從AnyObject
為UIButton!
因此您可以訪問標簽屬性。
@IBAction func favButtonDidTouch(sender: UIButton!) {
performSegueWithIdentifier("ArticleSegue", sender:NSIndexPath(forRow: sender.tag, inSection: 0))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.