[英]Delegate data from one UIViewController to another one
I am completely new to Swift programming and tried to delegate a single String
from one ViewController
to another by clicking a send button
. 我是Swift编程的新手,并试图通过单击发送
button
将一个String
从一个ViewController
委派给另一个。 The problem is , that it does not work ... 问题是,它不起作用...
I guess it would be easy for you to solve this and considering that it would be very helpful wether you explain me what I did wrong. 我想您可以轻松解决这个问题,并且考虑到如果您向我解释我做错了什么,那将非常有帮助。 :)
:)
Thank you a lot 非常感谢
import UIKit
protocol protoTYdelegate {
func didSendMessage(message: String)
}
class New: UIViewController {
@IBOutlet weak var SendButton: UIButton!
var tydelegate: protoTYdelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func SendButtonAction(_ sender: Any) {
let nachricht = "It works fine."
tydelegate?.didSendMessage(message: nachricht)
}
}
import UIKit
class ThankYouPage: UIViewController {
@IBOutlet weak var numbersView: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let controller = New()
controller.tydelegate = self
}
}
extension ThankYouPage: protoTYdelegate{
func didSendMessage(message: String) {
numbersView.text = message
}
As far as I understand, this code block doesn't work but the problem is not in the code, it's actually way that you choose to send data. 据我了解,此代码块不起作用,但问题不在代码中,这实际上是您选择发送数据的方式。 In iOS development, there are many ways to send data.
在iOS开发中,有多种发送数据的方法。 In your case, you need to use prepareForSegue method to send data to new class, not necessary to use delegates.
在您的情况下,您需要使用prepareForSegue方法将数据发送到新类,而不必使用委托。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ThankYouPage") {
let vc = segue.destination as! ThankYouPage
vc.message = "Message that you want to send"
}
}
And you need to implement your ThankYouPage as: 并且您需要将ThankYouPage实现为:
class ThankYouPage: UIViewController {
@IBOutlet weak var numbersView: UILabel!
var message = ""
override func viewDidLoad() {
super.viewDidLoad()
numbersView.text = message
}
}
In addition to that, you can use didSet method to print out the message to label instead of printing it directly in viewDidLoad method. 除此之外,您可以使用didSet方法打印出要标记的消息,而不是直接在viewDidLoad方法中打印。 Simply:
只是:
class ThankYouPage: UIViewController {
@IBOutlet weak var numbersView: UILabel!
var message: String?{
didSet{
numbersView.text = message
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I hope this helps you. 我希望这可以帮助你。
@Eyup Göymen
's answer is right. @Eyup Göymen
的答案是正确的。
I have another way, assuming that you are not using segue and you are pushing to next controller by manual-code. 我有另一种方法,假设您没有使用segue,而是通过手动代码推送到下一个控制器。
So your ThankYouPage
code should be like : 因此,您的
ThankYouPage
代码应类似于:
import UIKit
class ThankYouPage: UIViewController {
@IBOutlet weak var numbersView: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func someButtonAction(_ sender: Any) { // By clicking on some, you are opening that `New` controller
let detailView = self.storyboard?.instantiateViewController(withIdentifier: "New") as! New
detailView.tydelegate = self
self.navigationController?.pushViewController(detailView, animated: true)
}
}
extension ThankYouPage: protoTYdelegate {
func didSendMessage(message: String) {
numbersView.text = message
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.