我正在学习Swift并尝试使用Slider以编程方式更改Label值.我在func中收到错误,说paybackLabel是一个未解析的标识符.该怎么做?
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //Create Label let paybackLabel = UILabel(frame: CGRectMake(0, 0, 300, 41)) paybackLabel.center = CGPointMake(190, 284) paybackLabel.textAlignment = NSTextAlignment.Center paybackLabel.font = UIFont(name: paybackLabel.font.fontName, size: 40) paybackLabel.textColor = UIColor.whiteColor() paybackLabel.text = "Hello World" self.view.addSubview(paybackLabel) //Create Slider let paybackSlider = UISlider(frame: CGRectMake (45,546,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) self.view.addSubview(paybackSlider) } func paybackSliderValueDidChange(sender: UISlider!) { print("payback value: \(sender.value)") paybackLabel.text = "\(sender.value)" } }
Pavan Gandhi.. 6
正如我在这里建议的那样纠正你的代码.
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.)为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)" }
结果:
它的工作很好享受..