一个视图控制器上有两个文本标签,我想将标签中的文本连接到另一个视图控制器上相应的uibutton和UILabels。
现在,当我按下“thisUploadPhoto”按钮时,它会加载下一个屏幕的两个屏幕,而不是一个屏幕。如果你能帮我,那就太好了。另外,当我按下PhotoLabelViewController
上的“完成”和“取消”按钮时,会出现以下错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[tot.PhotoShareLabelViewController didTapDone:]:发送到实例0x7f8cecf5d0b0的未识别的选择器”
当它应该打印“完成”时
我不知道为什么下面的代码不能把两个视图控制器连接起来。
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”按钮从故事板连接到调用didTapDone
中名为PhotoShareLabelViewController
的方法。但是,您的didTapDone
方法是在PhotoShareViewController
中实现的。它是一个不同的视图控制器。
要解决此问题,请将方法实现放在正确的视图控制器中。然后,在情节串连板中选择“完成”按钮,转到其“连接检查器”[见下图]并删除其当前错误连接。然后,将其连接到正确视图控制器中的正确didTapDone
方法。
关于ios - 为什么数据没有在 View Controller 之间传递? swift 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45574754/