Swift 按ViewController时无法传输图像
我有tableView,其中有imageView和label,当我选择单元格时,我想推送另一个viewController并在其中显示我所选的单元格imageView.image和label.text,但在隐式展开图像行中的可选值时,出现了意外发现为零的错误Swift 按ViewController时无法传输图像,swift,Swift,我有tableView,其中有imageView和label,当我选择单元格时,我想推送另一个viewController并在其中显示我所选的单元格imageView.image和label.text,但在隐式展开图像行中的可选值时,出现了意外发现为零的错误 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vc = storyboard?.instantiateView
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController
vc?.pushedImage.image = UIImage(named: imagesArray[indexPath.row])
vc?.pushedLabel.text = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
在隐式展开可选值时意外地发现nil您发布的内容不多,但崩溃行似乎是 self.navigationController?.pushViewControllervc!,动画:真的 是否确定此情节提要?.InstanceEviewController的标识符:PushedViewController为?PushedViewController不是零 可能PushedViewController不在同一情节提要中。不管怎样,只要把它放在一个防护装置里,让它打开可选的
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { //fallback goes here return }
vc.pushedImage.image = UIImage(named: imagesArray[indexPath.row])
vc.pushedLabel.text = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
你发的帖子不多,但似乎有人在排队 self.navigationController?.pushViewControllervc!,动画:真的 是否确定此情节提要?.InstanceEviewController的标识符:PushedViewController为?PushedViewController不是零 可能PushedViewController不在同一情节提要中。不管怎样,只要把它放在一个防护装置里,让它打开可选的
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { //fallback goes here return }
vc.pushedImage.image = UIImage(named: imagesArray[indexPath.row])
vc.pushedLabel.text = imagesArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
在第一个视图中,控制器selectedImage和selectedText将在tableView didSelectRowAt方法中为零。只有在按下第二个视图控制器后,才会启动IBOutlets。因此,在第二个视图控制器中创建UIImage和字符串变量,并将值发送给这些变量。在第二个视图控制器的viewDidLoad方法中,将传递的值分配给IBOutlet 推式视图控制器
class PushedViewController: UIViewController {
@IBOutlet weak var pushedLabel: UILabel!
@IBOutlet weak var pushedImage: UIImageView!
var selectedImage: UIImage?
var selectedText: String?
override func viewDidLoad() {
super.viewDidLoad()
//vc?.pushedImage vc?.pushedLabel are not nil
pushedImage.image = selectedImage
pushedLabel.text = selectedText
}
}
在第一个视图中,控制器selectedImage和selectedText将在tableView didSelectRowAt方法中为零。只有在按下第二个视图控制器后,才会启动IBOutlets。因此,在第二个视图控制器中创建UIImage和字符串变量,并将值发送给这些变量。在第二个视图控制器的viewDidLoad方法中,将传递的值分配给IBOutlet 推式视图控制器
class PushedViewController: UIViewController {
@IBOutlet weak var pushedLabel: UILabel!
@IBOutlet weak var pushedImage: UIImageView!
var selectedImage: UIImage?
var selectedText: String?
override func viewDidLoad() {
super.viewDidLoad()
//vc?.pushedImage vc?.pushedLabel are not nil
pushedImage.image = selectedImage
pushedLabel.text = selectedText
}
}
UI组件尚未初始化,因此您无法在初始化控制器后立即访问它们 我建议您在PushedViewController中使用一个局部变量来存储图像和文本,然后在viewDidLoad中指定它们 像这样:
class PushedViewController: UIViewController {
//..
var image:UIImage?
var text:String?
//..
override viewDidLoad() {
super.viewDidLoad()
//..
if let image = self.image {
self.imageView.image = image
}
if let text = self.text {
self.label.text = text
}
//..
}
//..
}
然后从推送控制器的位置设置这些属性:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { return }
// Assign the properties here
vc.image = UIImage(named: imagesArray[indexPath.row])
vc.text = textsArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
UI组件尚未初始化,因此您无法在初始化控制器后立即访问它们 我建议您在PushedViewController中使用一个局部变量来存储图像和文本,然后在viewDidLoad中指定它们 像这样:
class PushedViewController: UIViewController {
//..
var image:UIImage?
var text:String?
//..
override viewDidLoad() {
super.viewDidLoad()
//..
if let image = self.image {
self.imageView.image = image
}
if let text = self.text {
self.label.text = text
}
//..
}
//..
}
然后从推送控制器的位置设置这些属性:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "PushedViewController") as? PushedViewController else { return }
// Assign the properties here
vc.image = UIImage(named: imagesArray[indexPath.row])
vc.text = textsArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
你在哪一行出错?你在哪一行出错?