一个网点委托后如何赋值?
How to assign value to an outlet after delegating it?
所以我的应用程序有 3 个屏幕:主屏幕、第二屏幕和结果屏幕(非常简单)
在主屏幕上,我显示了一个标签和用于更改它的按钮
在第二个上,我用 textinput 更改标签并将其传递给 Result
(有
导航控制器)
在最后一个屏幕上我显示了结果和 2 个按钮:保存和
取消
我的问题是我无法为 Main 的插座赋值,因为它为零,而且我无法对 viewDidLoad() 做任何事情,因为它只在应用程序启动时工作一次。
我该怎么做才能解决这个问题?是否有重新加载视图的功能,以便我可以在 viewDidLoad 中分配值?
Storyboard screenshot
整个应用程序在这里:https://drive.google.com/file/d/1mvL2fVxjOHbL4dReCwJ8poIq9G9-ezny/view
主VC:
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
第二个VC:
class SecondVC: UIViewController {
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
结果VC:
protocol ResultVCDelegate {
func passData(text: String)
}
class ResultVC: UIViewController {
var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
let mainVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainVC") as! MainVC
self.delegate = mainVC
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
顺便说一句:我用两个屏幕做了类似的应用程序,它的工作就像一个魅力......奇怪
-> Main VC(需要获取数据的地方)
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
print(text)
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
-> 结果VC(从你需要发送数据的地方)
protocol ResultVCDelegate: AnyObject {
func passData(text: String)
}
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = "Please Enter Something"
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
self.delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
根据我的评论。
查看您的代码,问题是您没有将对 MainVC 的引用传递给 ResultVC,而是创建了 MainVC 的新实例,这会导致崩溃,因为视图控制器没有已正确创建。但是您不想创建新实例,因为您需要对您创建的原始 MainVC 的引用。
您可以通过传递引用来获取它,您将需要更新所有三个 ViewController。这样的事情应该有效。请注意,我还没有对此进行测试,但这是一般原则。
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
label.text = text
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self // Here we add the delegate (the reference to MainVC)
present(nextVC, animated: true, completion: nil)
}
}
我们需要在此处添加委托,然后将其转发到 ResultVC。
class SecondVC: UIViewController {
weak var delegate: ResultVCDelegate? // Add the delegate that will hold a reference to MainVC
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
nextVC.delegate = delegate // Here we pass the reference to MainVC
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
我们可以从故事板中删除用于实例化 MainVC 的代码,而只是使用委托将值传回 MainVC。
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
// Just use the delegate no need to create a new instance
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
所以我的应用程序有 3 个屏幕:主屏幕、第二屏幕和结果屏幕(非常简单)
在主屏幕上,我显示了一个标签和用于更改它的按钮
在第二个上,我用 textinput 更改标签并将其传递给 Result (有
导航控制器)
在最后一个屏幕上我显示了结果和 2 个按钮:保存和 取消
我的问题是我无法为 Main 的插座赋值,因为它为零,而且我无法对 viewDidLoad() 做任何事情,因为它只在应用程序启动时工作一次。
我该怎么做才能解决这个问题?是否有重新加载视图的功能,以便我可以在 viewDidLoad 中分配值?
Storyboard screenshot
整个应用程序在这里:https://drive.google.com/file/d/1mvL2fVxjOHbL4dReCwJ8poIq9G9-ezny/view
主VC:
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
第二个VC:
class SecondVC: UIViewController {
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
结果VC:
protocol ResultVCDelegate {
func passData(text: String)
}
class ResultVC: UIViewController {
var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
let mainVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainVC") as! MainVC
self.delegate = mainVC
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
顺便说一句:我用两个屏幕做了类似的应用程序,它的工作就像一个魅力......奇怪
-> Main VC(需要获取数据的地方)
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
// label.text = text -- throws error
print(text)
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self
present(nextVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
-> 结果VC(从你需要发送数据的地方)
protocol ResultVCDelegate: AnyObject {
func passData(text: String)
}
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = "Please Enter Something"
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
self.delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}
根据我的评论。
查看您的代码,问题是您没有将对 MainVC 的引用传递给 ResultVC,而是创建了 MainVC 的新实例,这会导致崩溃,因为视图控制器没有已正确创建。但是您不想创建新实例,因为您需要对您创建的原始 MainVC 的引用。
您可以通过传递引用来获取它,您将需要更新所有三个 ViewController。这样的事情应该有效。请注意,我还没有对此进行测试,但这是一般原则。
class MainVC: UIViewController, ResultVCDelegate {
func passData(text: String) {
label.text = text
}
@IBOutlet weak var label: UILabel!
@IBAction func change(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavVC")
nextVC.delegate = self // Here we add the delegate (the reference to MainVC)
present(nextVC, animated: true, completion: nil)
}
}
我们需要在此处添加委托,然后将其转发到 ResultVC。
class SecondVC: UIViewController {
weak var delegate: ResultVCDelegate? // Add the delegate that will hold a reference to MainVC
@IBOutlet weak var inputText: UITextField!
@IBAction func save(_ sender: Any) {
let nextVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ResultVC") as! ResultVC
nextVC.labelText = inputText.text!
nextVC.delegate = delegate // Here we pass the reference to MainVC
navigationController?.pushViewController(nextVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
我们可以从故事板中删除用于实例化 MainVC 的代码,而只是使用委托将值传回 MainVC。
class ResultVC: UIViewController {
weak var delegate: ResultVCDelegate?
var labelText = ""
@IBOutlet weak var label: UILabel!
@IBAction func saveAndGoHome(_ sender: Any) {
// Just use the delegate no need to create a new instance
delegate?.passData(text: labelText)
dismiss(animated: true, completion: nil)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText.isEmpty ? label.text : labelText
}
}