prepare(for:sender:)方法失效?iOS页面传值类型转换报错
iOS颜色列表跳转类型转换错误问题
问题场景
开发一个双页面iOS应用:首页用UITableView展示随机颜色列表,点击列表项通过performSegue传递颜色到详情页,设置详情页背景色。但点击后触发类型转换错误,功能失效。
实现代码
import UIKit class ViewController: UIViewController { var color: [UIColor] = [] override func viewDidLoad() { super.viewDidLoad() createRandomColorArray() } func createRandomColorArray() { for _ in 0..<50 { color.append(createRandomColor()) } } func createRandomColor() -> UIColor { let randomColor = UIColor(red: CGFloat.random(in: 0...1.0), green: CGFloat.random(in: 0...1.0), blue: CGFloat.random(in: 0...1.0), alpha: 1) return randomColor } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if(segue.identifier == "ToColorDetailsVC") { if let destinationVC = segue.destination as? ColorDetailsVC { destinationVC.color = sender as? UIColor } } } } extension ViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return color.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "ColorCell") else { return UITableViewCell() } cell.backgroundColor = color[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt index indexPath: IndexPath) { let color = color[indexPath.row] performSegue(withIdentifier: "ToColorDetailsVC", sender: color) } }
错误日志
Could not cast value of type 'UIViewController' (0x10ea53d88) to 'color_app.ColorDetailsVC' (0x104cb8f88). 2023-11-07 00:21:54.111745+0600 color_app[32335:306275] Could not cast value of type 'UIViewController' (0x10ea53d88) to 'color_app.ColorDetailsVC' (0x104cb8f88). Could not cast value of type 'UIViewController' (0x10ea53d88) to 'color_app.ColorDetailsVC' (0x104cb8f88). CoreSimulator 857.13 - Device: iPhone 12 Pro Max (85A3BBC1-BE77-4A52-B71F-8ADD0F7911D1) - Runtime: iOS 16.1 (20B72) - DeviceType: iPhone 12 Pro Max
问题原因与修复步骤
核心原因
Storyboard中跳转的目标控制器未关联ColorDetailsVC类,系统默认将其识别为UIViewController,导致代码中类型转换失败。
修复步骤
- 打开Storyboard,找到详情页对应的视图控制器。
- 选中该控制器,打开右侧Identity Inspector(第三个标签页)。
- 在Class字段中选择或输入
ColorDetailsVC,确认模块(Module)为当前应用名称(通常自动填充)。 - 检查Segue标识符:确保Storyboard中Segue的ID与代码中的
ToColorDetailsVC完全一致。 - 验证
ColorDetailsVC类:确认项目中存在该类文件,且类名拼写、大小写完全正确(Swift大小写敏感)。
额外优化建议
- 在
prepare(for:sender:)中添加断言,方便调试:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "ToColorDetailsVC" else { return } guard let destinationVC = segue.destination as? ColorDetailsVC else { assertionFailure("目标控制器类型不匹配,请检查Storyboard配置") return } guard let selectedColor = sender as? UIColor else { assertionFailure("传递的颜色数据类型错误") return } destinationVC.color = selectedColor }
- 确保
ColorDetailsVC中正确定义并使用color属性:
class ColorDetailsVC: UIViewController { var color: UIColor? override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = color ?? .white } }
内容的提问来源于stack exchange,提问作者Sabbir33
相关产品推荐
相关产品推荐

