这段代码来自 locations tableView View Controller ,其中的位置是从 Four Square API 序列化的,我获取数据,将其传回我创建事件的 View Controller 。数据已正确序列化,填充了 tableView 和所有内容,所以我现在不打算发布它,只发布用于传递数据的 segue 方法。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForSelectedRow
let item = self.responseItems![indexPath!.row]
var eventVC = CreateEventViewController()
if segue.identifier == "pushBackToEventVC" {
if let venue = item["venue"] as? NSDictionary {
let locationString = venue["name"] as! String
let location = venue["location"]
//get lat/long strings
print(location)
eventVC.updateWithLocation(locationString)
eventVC.updateWithGeoPoint(PFGeoPoint(latitude: locationLatitude, longitude: locationLongitude))
}
eventVC = segue.destinationViewController as! CreateEventViewController
//pass location coordinates
}
//the rest of the segue method
}
crete 事件 View Controller 中的更新方法,当我在这些方法上放置断点时,我可以看到数据已正确传递,位置(一个 PFGeoPoint)工作正常,但 locationString(一个字符串)抛出一个“Bad instruction error”,尽管它确实将其正确打印到控制台
func updateWithLocation(locationString: String) {
print(locationString)
self.locationLabel.text = locationString
}
func updateWithGeoPoint(venueLocation: PFGeoPoint) {
// print(venueLocation)
self.event?.location = venueLocation
self.eventLocation = venueLocation
print(self.eventLocation)
}
有什么想法吗?它基本上可以工作,但不会用字符串更新标签,尽管我可以看到它已正确传递。感谢您一如既往的帮助。
最佳答案
尝试改用此代码块,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "pushBackToEventVC") {
let detailViewController = ((segue.destinationViewController) as! CreateEventViewController)
let indexPath = self.tableView!.indexPathForSelectedRow!
detailViewController.locationString = venue["location"]
}
}
关于ios - 从 API 发出传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755276/