如何更新从一个 class 委托给另一个的值?
How do you update the value you delegated from one class to the other?
我在委派方面遇到问题。我对这个概念还比较陌生,但从概念上讲我明白了,而且它很重要。我只是在使用它时遇到问题。我似乎无法将数据从一个 class 传递到另一个。我知道有一些关于堆栈溢出的委托示例,但它们并没有完全理解我的误解。我使用了协议、委托,并在 class 中调用它。我相信我只是遗漏了一些细微差别......而且我的代码中缺乏功能是显而易见的
//my protocol:
protocol StingHolder {
func StringPasser(ThisText text: String)
}
创建委托协议关系,放置要传递的数据然后关闭视图控制器
// my classes for placing data to be passed
class changeLabel: UIViewController,UITextFieldDelegate{
var Delegate: StingHolder?
@IBOutlet weak var TexrBeingPassed: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
TexrBeingPassed.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func ButtonPassingDataOtherView(_ sender: Any) {
Delegate?.StringPasser(ThisText: TexrBeingPassed.text!)
dismiss(animated: true, completion: nil)
}
}
创建更改标签 class 及其委托的实例并将其自身设置为委托 *应该更改标签,但它没有
///class to receive data
class ViewController: UIViewController{
@IBOutlet weak var LableName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
}
继承协议并告诉它将标签更改为更改标签class代表已通过的任何内容
// extension view controller inheriting the protocol
extension ViewController : StingHolder{
func StringPasser(ThisText text: String){
print("Delegate is working")
LableName.text = text
///
}
}
我想让一个视图控制器编辑另一个视图控制器的文本标签
您 self
作为委托的对象与屏幕上显示的对象不同。
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" is set...
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
// but "ViewController" is presented
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
labelChange
和 ViewController
是两个不同的独立对象。一个是直接调用init
创建的,一个是调用storyBoard.instantiateViewController
创建的。您应该设置 后者 的委托:
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" can be deleted
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
if let ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView") as? labelChange {
ViewController.delegate = self
self.present(ViewController, animated: true, completion: nil)
}
}
我在委派方面遇到问题。我对这个概念还比较陌生,但从概念上讲我明白了,而且它很重要。我只是在使用它时遇到问题。我似乎无法将数据从一个 class 传递到另一个。我知道有一些关于堆栈溢出的委托示例,但它们并没有完全理解我的误解。我使用了协议、委托,并在 class 中调用它。我相信我只是遗漏了一些细微差别......而且我的代码中缺乏功能是显而易见的
//my protocol:
protocol StingHolder {
func StringPasser(ThisText text: String)
}
创建委托协议关系,放置要传递的数据然后关闭视图控制器
// my classes for placing data to be passed
class changeLabel: UIViewController,UITextFieldDelegate{
var Delegate: StingHolder?
@IBOutlet weak var TexrBeingPassed: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
TexrBeingPassed.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func ButtonPassingDataOtherView(_ sender: Any) {
Delegate?.StringPasser(ThisText: TexrBeingPassed.text!)
dismiss(animated: true, completion: nil)
}
}
创建更改标签 class 及其委托的实例并将其自身设置为委托 *应该更改标签,但它没有
///class to receive data
class ViewController: UIViewController{
@IBOutlet weak var LableName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
}
继承协议并告诉它将标签更改为更改标签class代表已通过的任何内容
// extension view controller inheriting the protocol
extension ViewController : StingHolder{
func StringPasser(ThisText text: String){
print("Delegate is working")
LableName.text = text
///
}
}
我想让一个视图控制器编辑另一个视图控制器的文本标签
您 self
作为委托的对象与屏幕上显示的对象不同。
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" is set...
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
// but "ViewController" is presented
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
labelChange
和 ViewController
是两个不同的独立对象。一个是直接调用init
创建的,一个是调用storyBoard.instantiateViewController
创建的。您应该设置 后者 的委托:
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" can be deleted
}
@IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
if let ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView") as? labelChange {
ViewController.delegate = self
self.present(ViewController, animated: true, completion: nil)
}
}