为什么数据不在视图控制器之间传递? Swift 3

Why isn't the data passing between the view controllers? Swift 3

我在一个视图控制器上有 2 个文本标签,我想将标签中的文本连接到另一个视图控制器上相应的 UIButton 和 UILabel。

现在,当我按下 "thisUploadPhoto" 按钮时,它会加载下一个屏幕的 2 个屏幕,而不仅仅是一个屏幕。如果你能帮助我,那就太好了。此外,当我按下 PhotoLabelViewController 上的完成和取消按钮时,它会给我这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[tot.PhotoShareLabelViewController didTapDone:]: unrecognized selector sent to instance 0x7f8cecf5d0b0'

什么时候应该打印 'done'

我不知道为什么我的下面的代码无法将 2 个视图控制器连接在一起。

import UIKit

class PhotoShareViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var contentTextView: UITextView!

    @IBOutlet weak var thatTextField: UITextField!
    @IBOutlet weak var thisTextField: UITextField!
    var presenter: PhotoShareModuleInterface!
    var image: UIImage!

    @IBAction func thisUploadPhoto(_ sender: Any) {
        if thisTextField.text != "" && thatTextField.text != ""
        {
            performSegue(withIdentifier: "segue", sender: self)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var photoShareLabelViewController = segue.destination as! PhotoShareLabelViewController
        photoShareLabelViewController.thisString = thisTextField.text!
        photoShareLabelViewController.thatString = thatTextField.text!
    }

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

        imageView.image = image
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    @IBAction func didTapCancel(_ sender: AnyObject) {
        presenter.cancel()
        presenter.pop()
    }

    @IBAction func didTapDone(_ sender: AnyObject) {
        guard let message = thatTextField.text, !message.isEmpty else {
            return
        }
        guard let messageOne = thisTextField.text, !messageOne.isEmpty else {
            return
        }

        presenter.finish(with: image, content:message)
        presenter.dismiss()
    }
}

extension PhotoShareViewController: PhotoShareViewInterface {
    var controller: UIViewController? {
        return self
    }
}

import UIKit

class PhotoShareLabelViewController: UIViewController {
    @IBOutlet weak var thisLabel: UILabel!
    @IBOutlet weak var thatLabel: UILabel!
    @IBOutlet weak var thisButton: UIButton!
    @IBOutlet weak var thatButton: UIButton!

    var thisCounter = 0
    var thatCounter = 0
    var presenter: PhotoShareModuleInterface!
    var image: UIImage!

    @IBAction func pressedThisButton(_ sender: Any) {
        thisCounter += 1

        print(thisCounter)
    }

    @IBAction func pressedThatButton(_ sender: Any) {
        thatCounter += 1

        print(thatCounter)
    }

    var thisString = String()
    var thatString = String()

    @IBAction func pressedButtonDone(_ sender: Any) {
        print("done")
    }

    @IBAction func pressedButtonCancel(_ sender: Any) {
        print("cancel")
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        thisLabel.text = thisString
        thisButton.setTitle(thisString, for: UIControlState.normal)
        thatLabel.text = thatString
        thatButton.setTitle(thatString, for: UIControlState.normal)
    }

}

"Done" 按钮从 Storyboard 连线以调用 PhotoShareLabelViewController 中名为 didTapDone 的方法。但是,您的 didTapDone 方法是在 PhotoShareViewController 中实现的。这是一个不同的视图控制器。

要解决此问题,请将方法实现放在正确的视图控制器中。然后,select 故事板中的 "Done" 按钮并转到其 "Connections Inspector" [见下图] 并删除其当前的错误连接。然后,将其连接到正确的视图控制器中的正确 didTapDone 方法。