Swift:委派协议未正确设置 UILabel [英] Swift: Delegation protocol not setting UILabel properly

查看:70
本文介绍了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?

这里是 SentenceSentenceMarkov

https://gist.github.com/anonymous/9757d0ff00a4df7a29cb - 句子

https://gist.github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆