使用属于Singleton的变量更新UILabel.text

时间:2016-02-04 07:05:00

标签: ios swift

我有以下Singleton

class SharingManager{
  var smallBigText : String = "KLANG!"
  static let sharedInstance = SharingManager()
}

我用它来设置以下UILabel

的文字
 @IBOutlet weak var kleinGrossLabel: UILabel!

我在ViewController

中初始化其文字
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.kleinGrossLabel.text = SharingManager.sharedInstance.smallBigText
}

我在SharingManager.sharedInstance.smallBigText类的实例方法中重置了SoundEvent

   class SoundEvent {
    var text:String
    var duration:Double
    init(text: String, duration: Double){
      self.text = text
      self.duration = duration
    }

   func startEvent(){
    SharingManager.sharedInstance.smallBigText = self.text
   }

   func getDuration() -> Double{
      return self.duration 
    }
}

当我运行应用程序时,UILabel文本仍为“KLANG”,并且永远不会更改。

当我在以下函数中调用startEvent时应该更改它:

func playEvent(eventIndex : Int){
    if (eventIndex < 2){
        let currEvent = self.eventArray[eventIndex]
        currEvent?.startEvent()
        let nextIndex = eventIndex + 1
        //NSTimer.scheduledTimerWithTimeInterval(0.4, target: SomeClass.self, selector: Selector("someClassMethod"), userInfo: nil, repeats: true)
        NSTimer.scheduledTimerWithTimeInterval((currEvent?.duration)!, target: self, selector: Selector("playEvent:"), userInfo: NSNumber(integer: nextIndex), repeats: false)
    }
    else if (eventIndex==2){
        self.eventArray[eventIndex]?.startEvent()
        NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("sentenceDidFinish"), userInfo: nil, repeats: false)
    }
    else{
        //Do Nothing
    }
}

我在ViewController

中打电话给我
  var s1:Sentence = Sentence(type: "S3")
    s1.start()

Sentence课程中的内容是:

func start(){
    self.playEvent(0)

}

不知怎的,它打破了这个逻辑流,或者如果预期的事件序列正在执行,那么我在更改共享的Singleton资源{{1}时实际上并没有改变UILabel的文本。 } var

为清楚起见,所有主要smallBigText

https://gist.github.com/anonymous/07542f638fc5b9a3c4e9

https://gist.github.com/anonymous/10f5f0deb03f9adc354c

https://gist.github.com/anonymous/94fda980836dc057b05b

0 个答案:

没有答案