prepareForSegue 清空我的 NSManagedObject 数组
prepareForSegue empties my NSManagedObject Array
我需要在当前应用程序中将数据从一个视图传递到另一个视图我正在使用最新版本的 Swift for iOS 8. 我使用一个函数填充 FacultyArray使用 MagicalRecord 从 CoreData 获取信息。我遇到的问题是,当我尝试在 prepareForSegue 函数中使用填充数组时,结果发现它是空的。
import UIKit
import SwiftyJSON
class EspecialidadTVC: UITableViewController {
let defaults = NSUserDefaults.standardUserDefaults()
let endpoint = Connection()
var facultyList:Array<Faculty>? = TR_Faculty().get()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources thavaran be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//return self.facultyArray!.count
return (self.facultyList?.count)!
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("facultyCell", forIndexPath: indexPath)
cell.textLabel?.text = self.facultyList![indexPath.row].nombre
return cell
}
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false 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 == "presentMainMenuSegue" {
let splitViewController:MasterSVC = segue.destinationViewController as! MasterSVC
let indexpath = self.tableView.indexPathForSelectedRow
splitViewController.faculty = self.facultyList![indexpath!.row]
//splitViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
}
if segue.identifier == "logoutSegue" {
}
}
}
是否有更好的方法在视图之间发送 NSManagedObject?
更新
这是 MasterSVC 的目标代码。没有控制台错误,只是我的数组被清空,所以当我发送对象时,这是一个 nil 对象。
import UIKit
import Foundation
class MasterSVC: UISplitViewController {
var faculty:Faculty!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true
}
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?) {
}
}
更新 2
填充数组的方法
class TR_Faculty {
let dateFormatter = NSDateFormatter()
func store(json: JSON) {
//Clear previous data
Faculty.MR_truncateAll()
for (index,subJson):(String, JSON) in json {
let fac = Faculty.MR_createEntity()
fac?.id = Int(subJson["IdEspecialidad"].stringValue)
fac?.codigo = subJson["Codigo"].stringValue
fac?.nombre = subJson["Nombre"].stringValue
fac?.descripcion = subJson["Descripcion"].stringValue
fac?.updated_at = self.dateFormatter.dateFromString(subJson["updated_at"].stringValue)
}
NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
}
func get()-> Array<Faculty>?{
return Faculty.MR_findAll() as! Array<Faculty>
}
}
好吧,我遇到的问题是对服务的调用是异步的,因此在您执行到另一个视图控制器的 segue 之前,无法保证调用会完成。对我有用的是将 performSegueWithIdentifier
放在调用的成功部分。
作为给刚开始在移动应用程序中使用 API 的其他人的提示,当您想从您的应用程序调用 restful API 时,您应该拨打电话,等待它成功完成,然后填充 SQLite 数据库。之后您可以调用数据库来填充视图。
我需要在当前应用程序中将数据从一个视图传递到另一个视图我正在使用最新版本的 Swift for iOS 8. 我使用一个函数填充 FacultyArray使用 MagicalRecord 从 CoreData 获取信息。我遇到的问题是,当我尝试在 prepareForSegue 函数中使用填充数组时,结果发现它是空的。
import UIKit
import SwiftyJSON
class EspecialidadTVC: UITableViewController {
let defaults = NSUserDefaults.standardUserDefaults()
let endpoint = Connection()
var facultyList:Array<Faculty>? = TR_Faculty().get()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources thavaran be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//return self.facultyArray!.count
return (self.facultyList?.count)!
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("facultyCell", forIndexPath: indexPath)
cell.textLabel?.text = self.facultyList![indexPath.row].nombre
return cell
}
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false 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 == "presentMainMenuSegue" {
let splitViewController:MasterSVC = segue.destinationViewController as! MasterSVC
let indexpath = self.tableView.indexPathForSelectedRow
splitViewController.faculty = self.facultyList![indexpath!.row]
//splitViewController.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
}
if segue.identifier == "logoutSegue" {
}
}
}
是否有更好的方法在视图之间发送 NSManagedObject?
更新
这是 MasterSVC 的目标代码。没有控制台错误,只是我的数组被清空,所以当我发送对象时,这是一个 nil 对象。
import UIKit
import Foundation
class MasterSVC: UISplitViewController {
var faculty:Faculty!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true
}
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?) {
}
}
更新 2
填充数组的方法
class TR_Faculty {
let dateFormatter = NSDateFormatter()
func store(json: JSON) {
//Clear previous data
Faculty.MR_truncateAll()
for (index,subJson):(String, JSON) in json {
let fac = Faculty.MR_createEntity()
fac?.id = Int(subJson["IdEspecialidad"].stringValue)
fac?.codigo = subJson["Codigo"].stringValue
fac?.nombre = subJson["Nombre"].stringValue
fac?.descripcion = subJson["Descripcion"].stringValue
fac?.updated_at = self.dateFormatter.dateFromString(subJson["updated_at"].stringValue)
}
NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
}
func get()-> Array<Faculty>?{
return Faculty.MR_findAll() as! Array<Faculty>
}
}
好吧,我遇到的问题是对服务的调用是异步的,因此在您执行到另一个视图控制器的 segue 之前,无法保证调用会完成。对我有用的是将 performSegueWithIdentifier
放在调用的成功部分。
作为给刚开始在移动应用程序中使用 API 的其他人的提示,当您想从您的应用程序调用 restful API 时,您应该拨打电话,等待它成功完成,然后填充 SQLite 数据库。之后您可以调用数据库来填充视图。