我已经在我的NewsController类中指定了一个segue到FullNews Class,它在我添加prepareforsegue函数之前就已经工作了。添加后,只要第一行的线程断点发生prepareforsegue,segue就不再发生冻结。这是两个班级。代码在
之下import UIKit
class NewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate {
var parser: NSXMLParser = NSXMLParser()
var info: [newsarticle] = []
var postTitle: String = String()
var postDesc: String = String()
var eName: String = String()
var index: Int = Int()
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
override func viewDidLoad() {
let url:NSURL = NSURL(string: "http://brrsd.k12.nj.us/rss/News.xml")!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI namespaceURI: String?, qualifiedName qualifiedName: String?, attributes attributeDict: [String : String])
{
eName = elementName
if elementName == "item" {
postTitle = String()
postDesc = String()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return info.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if let reusedCell = tableView.dequeueReusableCellWithIdentifier("Cell") {
cell = reusedCell
} else {
cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell")
}
let news: newsarticle = info[indexPath.row]
if let label = cell.textLabel {
label.text = news.title
}
return cell }
func parser(parser: NSXMLParser, foundCharacters string: String) {
let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (!data.isEmpty) {
if eName == "title" {
postTitle += data
} else if eName == "description" {
postDesc += data
}
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
let newsart: newsarticle = newsarticle()
newsart.title = postTitle
newsart.description = postDesc
info.append(newsart)
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
index = indexPath.row
performSegueWithIdentifier("NewsTransfer", sender:self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let Destination: FullNews = segue.destinationViewController as! FullNews
Destination.info = info[index]
}
/*
// 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?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
import UIKit
class FullNews: UIViewController {
var info: newsarticle = newsarticle()
@IBOutlet weak var NewsTitle: UITableViewCell!
@IBOutlet weak var Desc: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NewsTitle.textLabel?.text = info.title
Desc.text = info.description
// 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?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
为什么会这样?
答案 0 :(得分:0)
不确定应用流程的流程,但也许您可以在执行if
之前使用if let
和prepareForSegue
检查segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "NewsTransfer" {
// additionally, you can print something here to see if this step worked
//print("segue.identifier is NewsTransfer")
if let destination = segue.destinationViewController as! FullNews {
//print("destination is FullNews")
destination.info = info[index]
}
}
}
还要确保您的segue已正确连接并命名为
EDIT 好像你已经创建了一个断点,因此它在断点处停止了。拖动左侧的蓝色箭头将其删除,代码将继续运行。