我收到一个错误:
Thread 1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)
尝试将从Photo库中选择的图像传递给另一个视图控制器.
源ViewController –
@IBAction func myButtonTapped(_ sender: UIButton)
{
let secondVC = storyboard?.instantiateViewController(withIdentifier: "secondVC") as! secondVC
secondVC.passedVName = VNameTextField.text!
secondVC.passedCcName = DropTextBox.text!
secondVC.passedVImage = VImageView.image!
navigationController?.pushViewController(secondVC, animated: true)
}
目的地ViewController –
class secondVC: UIViewController
{
@IBOutlet weak var DisplayPassedImage: UIImageView!
@IBOutlet weak var ccNameDisplayLabel: UILabel!
@IBOutlet weak var VNameDisplayLabel: UILabel!
var passedVName = ""
var passedCcName = ""
var passedVImage = UIImage()
override func viewDidLoad()
{
super.viewDidLoad()
VNameDisplayLabel.text = passedVName
ccNameDisplayLabel.text = passedCcName
DisplayPassedImage.image = passedVImage // Error- Thread 1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)
}
}
注意:’PassedVName’和’passedCcName’数据已成功传递到目标ViewController.如果我尝试传递’passedVImage’,我会收到错误.
最佳答案 您可以使用NotificationCenter将操作/消息传递给任何viewController.在这里你需要在AppDelegate(全局手段)中获取UIImage对象,这样你就可以在任何地方传递它.
防爆.
1)你在第一个VC
2)在firstVC中选择Image并将此图像添加到您在AppDelegate中拍摄的图像对象中
3)在firstVC中发布您要通过操作的通知
4)在SecondVC上添加观察
5)您可以从AppDelegate对象(全局对象)获取图像.
否则你可以使用Protocol.