简体   繁体   English

为什么数据没有在视图控制器之间传递? 迅捷3

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

I have 2 text labels on one view controller and I want to connect the text from the labels to the corresponding UIButtons and UILabels on another View Controller. 我在一个视图控制器上有2个文本标签,并且我想将标签上的文本连接到另一个视图控制器上的相应UIButtons和UILabel。

Right now when I press on the "thisUploadPhoto" button it loads 2 screens of the next screen, instead of just one screen. 现在,当我按下“ thisUploadPhoto”按钮时,它将加载下一个屏幕的2个屏幕,而不仅仅是一个屏幕。 If you could help me with that it would be great. 如果您可以帮助我,那就太好了。 Also, when I press the done and cancel buttons on the PhotoLabelViewController it gives me this error: 另外,当我按下PhotoLabelViewController上的完成和取消按钮时, PhotoLabelViewController此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[tot.PhotoShareLabelViewController didTapDone:]: unrecognized selector sent to instance 0x7f8cecf5d0b0' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[tot.PhotoShareLabelViewController didTapDone:]:无法识别的选择器已发送到实例0x7f8cecf5d0b0'

When it is supposed to print 'done' 当应该打印“完成”时

I don't know why my code below isn't working on connecting the 2 view controllers together. 我不知道为什么下面的代码无法将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)
    }

}

The "Done" button is wired from Storyboard to call a method called didTapDone in PhotoShareLabelViewController . 从Storyboard连接了“ Done”按钮,以调用PhotoShareLabelViewController名为didTapDone的方法。 However, your didTapDone method is implemented in PhotoShareViewController . 但是,您的didTapDone方法是在PhotoShareViewController实现的。 It's a different view controller. 这是一个不同的视图控制器。

To fix this, put the method implementation in the correct view controller. 要解决此问题,请将方法实现放入正确的视图控制器中。 Then, select your "Done" button in the Storyboard and go to its "Connections Inspector" [see image below] and remove its current wrong connection. 然后,在情节提要中选择“完成”按钮,然后转到其“连接检查器” [参见下图],并删除其当前错误的连接。 Then, wire it to the correct didTapDone method in the correct view controller. 然后,将其连接到正确的视图控制器中的正确didTapDone方法。

方法接线示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM