Swift segue不会将数据传递给旧的ViewController

时间:2015-07-24 15:04:34

标签: ios swift uitableview uitextfield segue

道歉,如果这是愚蠢的,我是编码的新手。找不到与此问题相关的先前提问的问题(代码显示是正确的。)

我正在制作基本的新闻Feed应用。

在主视图控制器上FeedsTableViewController.swift我有一个按钮,推送时会将其移动到AddFeedViewController.swift。

在AddFeedViewController上我有一个segue函数,从UITextField(RSS源的Web地址)获取文本,并在segue运行一个函数(AddNewFeed),将数据输入TableView单元格在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"
    }
*/

}

AddFeedViewController:

导入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)

}

}

2 个答案:

答案 0 :(得分:0)

FeedsTableViewController添加新方法(此处UIStoryboardSegue参数很重要),例如:

@IBAction func dismissToFeeds(segue:UIStoryboardSegue) {
    let addViewController = segue.sourceViewController as! AddFeedViewController

    // get data from addViewController
    let data = addViewController.feedUrl.text
    println("\(data)")
    // reload table view
}

AddFeedViewController控制标签的故事板中,您可以使用该按钮将视图关闭到展开图标:

see the third 'Exit' icon

第三个“退出”图标,然后选择dismissToFeeds操作。

答案 1 :(得分:0)

当数据源更新时,您希望调用reloadData来更新tableview。我想/猜这就是问题所在。

传统上,您希望在目标视图控制器中创建一个字段,而不是直接解析它:DestinationViewController可能还没有处于正确的生命周期中,只是FYI