Prepare(for segue:) 不在 Realm 中传递数据
Prepare(for segue: ) not passing data in Realm
prepare(for segue: )
函数运行,但数据没有发送到我的目的地ViewController。加载必须接收数据的 VC 的集合视图时出现以下错误:
Unexpectedly found nil while unwrapping an Optional value
我已经确认 segue 类型是 selection
segue,segue 标识符是正确的,并且视图可以显示虚拟数据。
第一个视图控制器(抽屉VC):
let realm = try! Realm()
var allDrawers: Results<Drawer>?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("goToTool segue")
performSegue(withIdentifier: "goToTool", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToTool" {
print("Prepare for segue from drawerVC ran") // this runs!
let destinationVC = segue.destination as! ToolVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedDrawer = allDrawers?[indexPath.row]
print("Selected drawer set to index path") // doesn't run :(
}
} else if segue.identifier == "goToEditCategory" {
_ = segue.destination as! EditCategoryVC
}
}
目标视图控制器(工具VC):
let realm = try! Realm()
var allTools: Results<Tool>?
var selectedDrawer: Drawer? {
didSet{
// doesn't run since selectedDrawer was never set :(
print("The selected drawer changed from \(oldValue) to \(selectedDrawer?.title)")
loadTools()
}
}
func loadTools() {
allTools = selectedDrawer?.tools.sorted(byKeyPath: "title", ascending: false)
toolCollectionView.reloadData() // this is where I get the optional value error
}
你犯了一个很常见的错误。
在目标视图控制器中,您正在访问 loadTools()
中的 table 视图,在您分配选定的抽屉时,该视图尚未连接。因此 table 视图是 nil
并导致崩溃。
一个解决方案是删除 didSet
观察者
var selectedDrawer: Drawer?
并加载 viewDidLoad
中的数据
func viewDidLoad() {
super.viewDidLoad()
if let drawer = selectedDrawer {
allTools = drawer.tools.sorted(byKeyPath: "title", ascending: false)
toolCollectionView.reloadData()
}
}
如果 segue 从单元格连接到目标视图控制器,请删除 didSelectRowAt
方法。
prepare(for segue: )
函数运行,但数据没有发送到我的目的地ViewController。加载必须接收数据的 VC 的集合视图时出现以下错误:
Unexpectedly found nil while unwrapping an Optional value
我已经确认 segue 类型是 selection
segue,segue 标识符是正确的,并且视图可以显示虚拟数据。
第一个视图控制器(抽屉VC):
let realm = try! Realm()
var allDrawers: Results<Drawer>?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("goToTool segue")
performSegue(withIdentifier: "goToTool", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToTool" {
print("Prepare for segue from drawerVC ran") // this runs!
let destinationVC = segue.destination as! ToolVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedDrawer = allDrawers?[indexPath.row]
print("Selected drawer set to index path") // doesn't run :(
}
} else if segue.identifier == "goToEditCategory" {
_ = segue.destination as! EditCategoryVC
}
}
目标视图控制器(工具VC):
let realm = try! Realm()
var allTools: Results<Tool>?
var selectedDrawer: Drawer? {
didSet{
// doesn't run since selectedDrawer was never set :(
print("The selected drawer changed from \(oldValue) to \(selectedDrawer?.title)")
loadTools()
}
}
func loadTools() {
allTools = selectedDrawer?.tools.sorted(byKeyPath: "title", ascending: false)
toolCollectionView.reloadData() // this is where I get the optional value error
}
你犯了一个很常见的错误。
在目标视图控制器中,您正在访问 loadTools()
中的 table 视图,在您分配选定的抽屉时,该视图尚未连接。因此 table 视图是 nil
并导致崩溃。
一个解决方案是删除 didSet
观察者
var selectedDrawer: Drawer?
并加载 viewDidLoad
func viewDidLoad() {
super.viewDidLoad()
if let drawer = selectedDrawer {
allTools = drawer.tools.sorted(byKeyPath: "title", ascending: false)
toolCollectionView.reloadData()
}
}
如果 segue 从单元格连接到目标视图控制器,请删除 didSelectRowAt
方法。