【问题标题】:How to reset state of initial ViewController?如何重置初始 ViewController 的状态?
【发布时间】:2017-02-08 17:14:48
【问题描述】:

我有 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 时,再次单击会添加来自相机的照片。如何重置数据?按下后退按钮时如何重置一组照片?

【问题讨论】:

  • 为此使用协议
  • @ pradeepchauhan_pc 我对此有点困惑。你能帮帮我吗?

标签: ios swift uiviewcontroller camera segue


【解决方案1】:

你可以在VC1中实现viewWillAppear,然后删除图像Array的所有元素。这样,当您再次点击 VC2 时,VC1 的图像数组将为空。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    Images.removeAll()
}

顺便说一句,考虑重命名您的实例变量,将初始字符小写,这是一个很好的做法。

【讨论】:

    【解决方案2】:

    使用带有委托的协议将数据发送回 您可以在委托函数中更新 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
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 2015-02-21
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2023-01-27
      • 2021-03-10
      • 2021-01-15
      相关资源
      最近更新 更多