Swift:委派协议未正确设置 UILabel

编程入门 行业动态 更新时间:2024-10-23 23:31:35
本文介绍了Swift:委派协议未正确设置 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下Protocol:

protocol SoundEventDelegate{ func eventStarted(text:String) }

我在这门课中称之为:

class SoundEvent { var text:String var duration:Double init(text: String, duration: Double){ self.text = text self.duration = duration } var delegate : SoundEventDelegate? func startEvent(){ delegate?.eventStarted(self.text) } func getDuration() -> Double{ return self.duration //TODO is this common practice? } }

我的 ViewController 符合:

class ViewController: UIViewController, SoundEventDelegate { //MARK:Properties @IBOutlet weak var beginButton: UIButton! @IBOutlet weak var kleinGrossLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //DELEGATE method func eventStarted(text:String){ kleinGrossLabel.text = text } //MARK: actions @IBAction func startImprovisation(sender: UIButton) { var s1:Sentence = Sentence(type: "S3") var s2:Sentence = Sentence(type: "S1") var newModel = SentenceMarkov(Ult: s1, Penult: s2) s1.start() beginButton.hidden = true } }

但是当我运行应用程序时 kleinGrossLabel.text 不会改变.我是否以错误的方式提及标签?还是我委托的方式不对?

But when I run the app kleinGrossLabel.text does not change. Am I referring to the label in the wrong way? Or is it the way that I do delegation that is incorrect?

这里是 Sentence 和 SentenceMarkov

gist.github/anonymous/9757d0ff00a4df7a29cb - 句子

gist.github/anonymous/91d5d6a59b0c69cba915 - SentenceMarkov

推荐答案

首先,在 swift 中使用 setter 并不常见.如果你想拥有一个只读属性,你可以使用 private(set) var propertyName在其他情况下,只需访问评论中提到的属性

First off it's not common practice to have a setter in swift. if you want to have a readonly property you can use private(set) var propertyName in other cases simply access the property like mentioned in the comment

另外,我不明白为什么你在句子中 eventArray 的类型是 [SoundEvent?] 而不是 [SoundEvent] 作为 SoundEvent 似乎没有可失败的初始化程序

Also i don't see a reason why you eventArray in sentence is of type [SoundEvent?] not [SoundEvent] as SoundEventdoes not seem to have a failable initialiser

如前所述,您不仅需要实现 SoundEventDelegate 协议,还需要设置委托

Like mentioned before you need to not only implement the SoundEventDelegate protocol but also set the delegate

问题是您无法从视图控制器中真正访问 SoundEventDelegate,因为您在 Sentence

the problem is that you can't really access the SoundEventDelegate from the viewcontroller because you instantiate the SoundEvents inside Sentence

var soundEventDelegate: SoundEventDelegate?

最简单的方法是为句子添加一个 soundEventDelegate 属性并将其设置如下:

the easiest way to do this would be adding a soundEventDelegate property for sentence and setting it like this:

let s1:Sentence = Sentence(type: "S3") let s2:Sentence = Sentence(type: "S1") s1.soundEventDelegate = self s2.soundEventDelegate = self

在声音内部,您需要将每个事件的委托设置为 Sentence

and inside sound you would need the set the delegate for every event to the soundEventDelegate of Sentence

你可以这样做:

var soundEventDelegate: SoundEventDelegate? = nil { didSet { eventArray.forEach({$0.delegate = soundEventDelegate}) } }

或者编写另一个接受委托的初始化器

or write another initialiser that takes the delegate

希望能帮到你

ps:你不应该在 swift 中继承表单 NSObject 除非它真的有必要

p.s: you shouldn't inherit form NSObject in swift excepts it's really necessary

更多推荐

Swift:委派协议未正确设置 UILabel

本文发布于:2023-11-26 20:17:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   协议   Swift   UILabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!