抱歉,如果这很愚蠢,我是编码新手。找不到与此问题相关的先前提出的问题(代码似乎是正确的)。
我正在构建一个基本的新闻源应用程序。
在主视图 Controller FeedsTableViewController.swift 上,我有一个按钮,当按下该按钮时,该按钮会转到 AddFeedViewController.swift。
在 AddFeedViewController 上,我有一个 segue 函数,应该从 UITextField(RSS feed 的网址)获取文本,并在 segue 上运行一个将数据输入到 TableView 单元格的函数 (AddNewFeed)在 FeedsTableViewController 上。
运行应用程序后,我可以从 FeedsTableViewController 中返回,但没有数据传递到 FeedsTableViewController 中的单元格中。我知道 AddNewFeed 工作正常,因为我在 viewDidLoad 中自动运行该函数以在 Apple 的 RSS 提要中进行硬编码。
我唯一能想到的是数据没有正确地从 UITextField 中提取,或者没有作为参数正确输入到 AddNewFeed 中。不知道为什么这不起作用,非常感谢您的想法。
FeedsTableViewController 代码:
var feedUrl
override func viewDidLoad() {
super.viewDidLoad()
AddNewFeed("http://developer.apple.com/news/rss/news.rss")
}
func AddNewFeed(url: String) {
feedUrl = url
let url : NSURL = NSURL(string: feedUrl)!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
}
AddFeedViewController代码:
class AddFeedViewController: UIViewController {
@IBOutlet weak var feedUrl: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let feedsViewController = segue.destinationViewController as! FeedsTableViewController
feedsViewController.AddNewFeed(feedUrl.text)
}
}
这是 2 个类文件中的代码:
FeedsTableViewController:
导入UIKit
FeedsTableViewController 类:UITableViewController、NSXMLParserDelegate {
var parser : NSXMLParser = NSXMLParser()
var feedUrl : String = String()
var feedTitle : String = String()
var articleTitle : String = String()
var articleLink : String = String()
var articlePubDate : String = String()
var parsingChannel : Bool = false
var eName : String = String()
var feeds : [FeedModel] = []
var articles : [ArticleModel] = []
override func viewDidLoad() {
super.viewDidLoad()
AddNewFeed("http://developer.apple.com/news/rss/news.rss")
}
func AddNewFeed(url: String) {
feedUrl = url
let url : NSURL = NSURL(string: feedUrl)!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func retrieveNewsFeed(segue: UIStoryboardSegue){
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
eName = elementName
if elementName == "channel" {
feedTitle = String()
feedUrl = String()
parsingChannel = true
} else if elementName == "item" {
articleTitle = String()
articleLink = String()
articlePubDate = String()
parsingChannel = false
}
}
func parser(parser: NSXMLParser, foundCharacters string: String?) {
let data = string!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if(!data.isEmpty){
if parsingChannel {
if eName == "title" {
feedTitle += data
}
} else {
if eName == "title" {
articleTitle += data
} else if eName == "link" {
articleLink += data
} else if eName == "pubDate" {
articlePubDate += data
}
}
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "channel" {
let feed : FeedModel = FeedModel()
feed.title = feedTitle
feed.url = feedUrl
feed.articles = articles
feeds.append(feed)
} else if elementName == "item" {
let article : ArticleModel = ArticleModel()
article.title = articleTitle
article.link = articleLink
article.pubDate = articlePubDate
articles.append(article)
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return feeds.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedCell", forIndexPath: indexPath) as! UITableViewCell
let feed : FeedModel = feeds[indexPath.row]
cell.textLabel!.text = feed.title
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowArticles" {
let viewController: ArticlesTableViewController = segue.destinationViewController as! ArticlesTableViewController
let indexPath = self.tableView.indexPathForSelectedRow()!
let feed = feeds[indexPath.row]
viewController.articles = feed.articles
}
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
/*
if(segue.identifier == "SomeSegue"){
var articlesController = segue.destinationViewController as! ArticlesTableViewController
// gives access to the temp variable on the destination, now its ready to fire.
articlesController.temp = "hello there"
}
*/
}
添加FeedViewController:
导入UIKit
类AddFeedViewController:UIViewController {
@IBOutlet weak var feedUrl: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let feedsViewController = segue.destinationViewController as! FeedsTableViewController
feedsViewController.AddNewFeed(feedUrl.text)
}
}
最佳答案
当数据源更新时,需要调用reloadData来更新tableview。我认为/猜测这就是问题所在。
传统上,您希望在目标 View Controller 中创建一个字段,而不是直接解析它:DestinationViewController 可能尚未处于正确的生命周期,仅供引用
关于ios - Swift segue 不会将数据传递给旧的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613909/