我有2个ViewController:VC1
和VC2
。在VC1
我:
@IBAction func cliclOnBtn(_ sender: UIButton) {
CameraController.takePicture()
}
...
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMediaWithInfo", picker.sourceType)
let _image = (info[UIImagePickerControllerOriginalImage] as! UIImage)
ImageView.image = _image
Images.append(_image)
}
在VC2
我正在传递_image
数组,但是当我尝试重新启动VC1
时,再次点击即可添加来自相机的照片。如何重置数据?当按下后退按钮时,如何重置照片数组?
答案 0 :(得分:1)
您可以在VC1中实现viewWillAppear
,然后删除图像阵列的所有元素。这样,当您点击VC2时,VC1会将其图像阵列清空。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Images.removeAll()
}
顺便说一句,考虑重命名实例变量,降低初始字符,这是一个很好的做法。
答案 1 :(得分:0)
使用protocol with delegate发送数据 您可以在委托功能
中更新UI或代码import UIKit
protocol DestinationViewControllerDelegate {
func updateData(text:String); // you can use Array, Dictonary, Modal as per your requirment
}
class DestinationViewController: UIViewController {
var delegate:FullCalendarViewDelegate! = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func doneButton(sender: AnyObject) {
delegate?.updateData("Got new data")
self.navigationController?.popViewControllerAnimated(true)
}
}
//发件人视图控制器
class SenderViewController: UIViewController, DestinationViewControllerDelegate {
@IBOutlet weak var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func NextButton(sender: AnyObject) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let dash:DestinationViewControllerDelegate = storyBoard.instantiateViewControllerWithIdentifier("DestinationViewControllerDelegate") as! DestinationViewControllerDelegate
dash.delegate = self
self.navigationController?.pushViewController(dash, animated: true)
}
func updateData(text:String) {
titleLabel.text = text
}
}