简体   繁体   English

将数据从一个UIViewController委派给另一个UIViewController

[英]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.

相关问题 如何通过UITapGestureRecognizer将数据从一个UIViewController传递到另一个UIViewController - How to pass data from one UIViewController to another one through UITapGestureRecognizer 在另一个UIViewController中使用一个UIViewController中的UIView? - Using an UIView from one UIViewController in another UIViewController? 设置委托以将数据从一个选项卡传递到另一个选项卡 - Setting delegate to pass data from one tab to another 委托/协议将数据从一个视图控制器传递到另一个 - Delegate/Protocols Passing data from one view controller to another 如何使用委托将数据从一个视图控制器传递到另一个视图控制器? - how to use delegate to pass data to from one View Controller to another? 如何从一个Containerview传递到另一个UIViewController - How to pass from one Containerview to Another UIViewController 将图像从一个UIViewController发送到另一个 - Send image from one UIViewController to another 在Swift中以编程方式从一个UIViewController移至另一个 - Moving from one UIViewController to another in Swift programmatically 如何从另一个事件发送事件到UIViewController - How to send an event to UIViewController from another one 如何将字符串从一个uiviewcontroller传递到另一个 - how to pass a string from one uiviewcontroller to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM