Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每隔一段时间通过Swift segue传递数据_Swift - Fatal编程技术网

每隔一段时间通过Swift segue传递数据

每隔一段时间通过Swift segue传递数据,swift,Swift,我有一个tableview,它可以阅读和RSS订阅偶发的广播节目。当选定单元格时,我希望所选节目的播放列表传递给第二个控制器,以便在文本视图中查看。我使用的是segue,当我两次(每隔一次)选择同一个单元格时,它就会工作。我到处都找不到成功的地方,这让我发疯!请帮忙。这是我的密码 // Only grab the data at the selected cell // override func tableView(tableView: UITableView

我有一个tableview,它可以阅读和RSS订阅偶发的广播节目。当选定单元格时,我希望所选节目的播放列表传递给第二个控制器,以便在文本视图中查看。我使用的是segue,当我两次(每隔一次)选择同一个单元格时,它就会工作。我到处都找不到成功的地方,这让我发疯!请帮忙。这是我的密码

     // Only grab the data at the selected cell
        //
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        // Load the variable to hold whats in the row
        currentList = feeds.objectAtIndex(indexPath.row).objectForKey("itunes:summary") as NSString

        // Load the row number
        myRow = (indexPath.row)

    }

    // Pass the data thru the segue
    override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
        if (segue.identifier == "mySegue") {

//            var vc = segue.destinationViewController as secondViewController
//            vc.toPass = currentList
//            println(vc.toPass)



            let vc = segue.destinationViewController as secondViewController
            let indexPath = self.tableView.indexPathForSelectedRow
            vc.toPass = currentList
        }
    }
}
这是我的第二个视图控制器中的代码

 import UIKit

class secondViewController: UIViewController {

    // Create a property to accept the data

    @IBOutlet weak var textPlayList: UITextView!



    // Create a variable to store the data

    var toPass:String!

    override func viewDidLoad() {
        super.viewDidLoad()



        textPlayList.text = toPass
        textPlayList.textColor = UIColor .whiteColor()
        textPlayList.font = UIFont .boldSystemFontOfSize(10)

    }


}

问题是
prepareforsgue
发生在
didselectrowatinexpath
之前,因此
currentList
变量设置太晚,无法在
prepareforsgue
中使用

要解决此问题,请移动此代码:

    // Load the variable to hold whats in the row
    currentList = feeds.objectAtIndex(indexPath.row).objectForKey("itunes:summary") as NSString
prepareForSegue

    override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
        if (segue.identifier == "mySegue") {
            let vc = segue.destinationViewController as secondViewController
            if let indexPath = self.tableView.indexPathForSelectedRow() {
                let currentList = feeds.objectAtIndex(indexPath.row).objectForKey("itunes:summary") as NSString
                vc.toPass = currentList
            }
        }
    }

一般来说,如果您使用segues,您不需要
DidSelectRowatineXpath
,因为
prepareForSegue
是设置转换的地方。

我尝试过,但得到了一个错误:在PrepareForSegueee更新答案中放置此分配时,使用了未解析标识符“indexPath”。您需要调用
indexPathForSelectedRow
来获取
indexPathForSelectedRow
。您确实做到了这一点,并且获取:(UITableView,numberOfRowsInSection:Int)->Int'没有名为'indexPathForSelectedRow'的成员。斯威夫特有什么变化吗?听起来很奇怪。我建议您将表视图控制器代码作为一个新问题发布。在键入我的新问题时了解到:我使用的是一个表视图,而不是一个表视图控制器,因此我必须用表名替换“self:)