ios - 为什么数据不在 View Controller 之间传递? swift 3

标签 ios swift uilabel sigabrt

我在一个 View Controller 上有 2 个文本标签,我想将标签中的文本连接到另一个 View Controller 上相应的 UIButton 和 UILabel。

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

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

什么时候应该打印'done'

我不知道为什么我的下面的代码无法将 2 个 View Controller 连接在一起。

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)
    }

}

最佳答案

“完成”按钮从 Storyboard 连线以调用 PhotoShareLabelViewController 中名为 didTapDone 的方法。但是,您的 didTapDone 方法是在 PhotoShareViewController 中实现的。这是一个不同的 View Controller 。

要解决此问题,请将方法实现放在正确的 View Controller 中。然后,在 Storyboard 中选择“完成”按钮并转到其“连接检查器”[见下图]并删除其当前的错误连接。然后,将它连接到正确的 View Controller 中的正确的 didTapDone 方法。

example method wiring

关于ios - 为什么数据不在 View Controller 之间传递? swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574754/

相关文章:

ios - OpenCV iOS - 预期标识符(MACRO)

ios - TableView 不在 Swift 中注册点击,自定义单元格不显示

ios - iPhone解锁后如何停止本地通知声音,Swift 4

ios - 具有自动布局的 UIScrollView 内的动态 UILabel 高度

ios - 向下滚动表格 View 时如何使自定义标签作为 NavBar 消失?

ios - 使用 Swift 多次触摸 UIButton?

ios - 获取音频队列缓冲区的 <AudioTimeStamp>

objective-c - 监控多个全局 NSEventMask

swift - 如何在不使用 UITextView 的 UILabel 中检测和创建可点击链接

ios - 在 swift 中使用 ACEAutocompleteBar 崩溃 :