我正在学习Swift并尝试使用Slider以编程方式更改Label值。我在func中收到错误,说paybackLabel是一个未解析的标识符。该怎么做?
proto.createdCallback = function() {
let root = this.createShadowRoot();
root.innerHTML = "<input id='input' type='text'>"
+ "<br>"
+ "Result: <span id='result'></span>"
+ "<br><button onclick='this.parentNode.process()'>Run</button>";
this.shadowRoot.process = function() {
let spanEle = this.querySelector('span');
let inputEle = this.querySelector('input');
spanEle.textContent = performAlgorithm(inputEle.value.split(','));
};
};
document.registerElement('custom-ele', { prototype: proto });
答案 0 :(得分:5)
正如我在此建议的那样纠正你的代码。
1。)为Outside公开定义两个变量以公开访问和修改标签值
var paybackLabel : UILabel = UILabel()
var paybackSlider : UISlider = UISlider()
2。)将以下代码放在viewDidload()
中//Create Label
paybackLabel = UILabel(frame: CGRectMake(10, 20, 100, 41))
paybackLabel.center = CGPointMake(190, 61)
paybackLabel.textAlignment = NSTextAlignment.Center
paybackLabel.font = UIFont(name: paybackLabel.font.fontName, size:10)
paybackLabel.textColor = UIColor.blueColor()
paybackLabel.text = "Hello World"
self.view.addSubview(paybackLabel)
//Create Slider
paybackSlider = UISlider(frame: CGRectMake (45,70,310,31))
paybackSlider.minimumValue = 0
paybackSlider.maximumValue = 1000
paybackSlider.continuous = true
paybackSlider.tintColor = UIColor.blueColor()
paybackSlider.value = 500
paybackSlider.addTarget(self, action: "paybackSliderValueDidChange:",forControlEvents: .ValueChanged)
paybackLabel.text = "\(paybackSlider.value)"
self.view.addSubview(paybackSlider)
3。)不要通过更改slidervaluechanged事件
来放置在标签上显示值的更改功能func paybackSliderValueDidChange(sender: UISlider!)
{
print("payback value: \(sender.value)")
paybackLabel.text = "\(sender.value)"
}
结果:
现在它的工作很好享受..
答案 1 :(得分:2)
在ViewDidLoad()
之外全局声明 var paybackLabel = UILabel()
var paybackSlider = UISlider()
并且不要忘记将userInteractionEnabled设置为true
paybackLabel.userInteractionEnabled = true
paybackSlider.userInteractionEnabled = true