如何重置初始ViewController的状态?

时间:2017-02-08 17:14:49

标签: ios swift uiviewcontroller camera segue

我有2个ViewController:VC1VC2。在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时,再次点击即可添加来自相机的照片。如何重置数据?当按下后退按钮时,如何重置照片数组?

2 个答案:

答案 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
     }
}