gpt4 book ai didi

swift - 通过 Swift segue 传递数据每隔一段时间就有效

转载 作者:行者123 更新时间:2023-11-28 07:14:37 25 4
gpt4 key购买 nike

我有一个 tableview,它读取和 RSS feed 的情景广播节目。我希望所选节目的播放列表传递给第二个 Controller ,以便在选择单元格时在 TextView 中查看。我正在使用 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
}
}
}

这是我的第二个 View Controller 的代码

 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)

}


}

最佳答案

问题是 prepareForSegue 发生在 didSelectRowAtIndexPath 之前,因此您的 currentList 变量设置得太晚,无法在 prepareForSegue< 中使用.

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

    // 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
}
}
}

一般来说,如果您正在使用 segue,则不需要 didSelectRowAtIndexPath,因为 prepareForSegue 是您设置过渡的地方。

关于swift - 通过 Swift segue 传递数据每隔一段时间就有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26846804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com