gpt4 book ai didi

swift - 快速使用委托(delegate)将字符串从一个 View Controller 发送到另一个 View Controller

转载 作者:行者123 更新时间:2023-11-30 10:26:37 25 4
gpt4 key购买 nike

我试图在 PopupViewController 中单击按钮,并用字符串“svimmelhed,”填充 DagbogsindlaegViewController 中的文本字段。

运行代码时我没有收到任何错误,但当我运行应用程序模拟并按 PopupViewController 类中的“ButtonPressSvimmelhed”时,文本不存在。有没有一个好心的陌生人可以帮助我并告诉我我做错了什么?

代码如下:

import UIKit
import EventKit
class DagbogsindlaegViewController: UIViewController{

@IBAction func BackToSVC(_ sender: Any){
self.performSegue(withIdentifier: "BackToSVCSegue", sender: self)
}

@IBAction func ToCalenderButtonPress(_ sender: Any) {
self.performSegue(withIdentifier: "ToCalenderSegue", sender: self)
}

override func viewDidLoad() {
super.viewDidLoad()
TitleTextField.delegate = self
DescriptionTextField.delegate = self


// Do any additional setup after loading the view.
}

@IBOutlet weak var TitleTextField: UITextField!
@IBOutlet weak var DescriptionTextField: UITextField!

func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
let eventStore = EKEventStore()

eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
completion?(false, e)
return
}
completion?(true, nil)
} else {
completion?(false, error as NSError?)
}
})
}

@IBAction func ButtonPressGemDagbog(_ sender: Any) {
addEventToCalendar(title: "\(String(describing: TitleTextField.text!))", description: "\(String(describing: DescriptionTextField.text!))", startDate: NSDate() as Date, endDate: NSDate() as Date)
}

@IBAction func ButtonPressPopupMenu(_ sender: Any) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopupViewControllerID") as! PopupViewController
self.addChild(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: self)
}

}

extension DagbogsindlaegViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

extension DagbogsindlaegViewController : TextFraPopupDelegate {
func Symptomer(Svimmelhed: String) {
TitleTextField.text = Svimmelhed
}
}

另一个 View Controller :

import UIKit

protocol TextFraPopupDelegate{
func Symptomer(Svimmelhed: String)
}

class PopupViewController: UIViewController {

var symptomDelegate: TextFraPopupDelegate?

override func viewDidLoad() {
super.viewDidLoad()

self.showAnimate()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
// Do any additional setup after loading the view.
}

@IBAction func ButtonPressBackToDagbogsindlaeg(_ sender: Any) {
self.removeAnimate()

//self.view.removeFromSuperview()
}
func showAnimate()
{
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
});
}

func removeAnimate()
{
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}

@IBAction func ButtonPressSvimmelhed(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true
}
symptomDelegate?.Symptomer(Svimmelhed: "Svimmelhed, ")


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

}


}

最佳答案

正如评论中提到的,委托(delegate)未设置。

然而,在 Swift 中,有一种更方便的方法将字符串传递回呈现的 View Controller ,即回调闭包。您摆脱了协议(protocol)和设置委托(delegate)的责任

  • 删除

<罢工>

<罢工>
protocol TextFraPopupDelegate{
func Symptomer(Svimmelhed: String)
}

<罢工>

  • PopupViewController替换

    var symptomDelegate: TextFraPopupDelegate?

    var callback : ((String) -> Void)?

    @IBAction func ButtonPressSvimmelhed(_ sender: UIButton) {
    if sender.isSelected {
    sender.isSelected = false
    } else {
    sender.isSelected = true
    }
    symptomDelegate?.Symptomer(Svimmelhed: "Svimmelhed, ")
    }

    @IBAction func ButtonPressSvimmelhed(_ sender: UIButton) {
    sender.isSelected.toggle()
    callback?("Svimmelhed, ")
    }
  • DagbogsindlaegViewControllerButtonPressPopupMenu添加闭包

    @IBAction func ButtonPressPopupMenu(_ sender: Any) {
    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopupViewControllerID") as! PopupViewController
    self.addChild(popOverVC)
    popOverVC.view.frame = self.view.frame
    popOverVC.callback = { string in
    self.TitleTextField.text = string
    }
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParent: self)
    }
  • 最后删除

<罢工>

<罢工>
 extension DagbogsindlaegViewController : TextFraPopupDelegate {
func Symptomer(Svimmelhed: String) {
TitleTextField.text = Svimmelhed
}
}

<罢工>

注释:

  • 语法 (_ success: Bool, _ error: NSError?) -> Void)?已经过时了。自 Swift 3 以来,参数标签消失了,这就足够了: (Bool, NSError?) -> Void)?

  • 不要使用NS... Swift 中的类(如果有 native 对应项,例如 Date)

  • 请遵守命名约定,以小写字母开头来命名变量和函数/方法。

  • 语法 "\(String(describing: TitleTextField.text!))"是双重冗余的。您可以从字符串创建字符串。将其替换为 TitleTextField.text!

关于swift - 快速使用委托(delegate)将字符串从一个 View Controller 发送到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60008326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com