Swift:从另一个类更改uilabel文本

时间:2018-07-19 12:37:40

标签: ios swift uilabel

  

Index.swift

let boxClass = SelectBox()

override func viewDidLoad() {
    super.viewDidLoad()
    reloadData()
    boxClass.openSelector(defValue: myView)
}

func reloadData(){
    self.nowOnline.text = "Waiting ..." 
    countOnline = 2 
}

此代码可以正常工作。

但是,如果我想更改另一个类的值,则不起作用

  

SelectBox.swift

class SelectBox {
    internal func openSelector(myView:UIView){
        myView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClick(sender:))))
    }
}

@objc func handleClick(sender: UITapGestureRecognizer) {
    reloadData()
}

代码故事:

1-我要进入索引应用

2- reloadData()工作正常

3-我点击myView,然后再次reloadData()致电

4-应用程序崩溃,因为nowOnline返回了nil

1 个答案:

答案 0 :(得分:1)

class SelectBox {
   let indexObj = index()
    internal func openSelector(myView:UIView){
        myView.addGestureRecognizer(UITapGestureRecognizer(target: self, action:    #selector(handleClick(sender:))))
    }

@objc func handleClick(sender: UITapGestureRecognizer) {
    indexObj.reloadData()
  }
}