ViewController有一个名为userInput的textField,允许用户添加一个值,所以我想在用户按下calculate键时更改标签名称。我用类来修改标签名称,当从类xcode中调用viewController标签时显示" viewController没有成员名称userAnswer"但我的代码确实有一个名为userAnswer的标签。
import UIKit
class ViewController2: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet var userInput: UIPickerView!
@IBOutlet var userTo: UIPickerView!
@IBOutlet var userValue: UITextField!
@IBAction func calculate(sender: AnyObject) {
var textboxValue = userValue.text.toInt()
if textboxValue != nil {
if selection1 == 0 {
let milimeter = Milimeter()
milimeter.toCnverter(textboxValue!, item: selection2)
userAnswer.text = " "
} else if selection1 == 1 {
} else if selection1 == 2 {
} else if selection1 == 3 {
} else if selection1 == 4 {
}
} else {
userAnswer.text = "Please enter a Value"
}
}
@IBOutlet var userAnswer: UILabel!
let length1 = ["Milimeter","Centimeter","Inch","Meter","Kilometer"]
let length2 = ["Milimeter","Centimeter","Inch","Meter","Kilometer"]
var selection1 = 0
var selection2 = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
userInput.delegate = self
userInput.dataSource = self
userTo.delegate = self
userTo.dataSource = self
}
class Milimeter {
func toCnverter(value: Int, item: Int) {
switch item {
case 0:
userAnswer.text = " " // i can't access , shows error
case 1:
println("Inch")
case 2:
println("Kilometer")
default:
println(" ")
}
}
}
}
答案 0 :(得分:1)
您似乎忘记了连接UILabel
中的UIStoryboard
和代码中的UILabel
。要100%确定,请从代码中删除该变量,然后将UIStoryboard
的插座重新连接到您的代码。
看完整个代码后进行编辑:
您的class
毫米不知道您的UILabel
,因为它不是UIViewController
的属性,而是func toConverter
的属性。
我的建议:func toConverter(value: Int, item: Int) -> String
应该返回它转换的内容。这使得它也可以测试 - > if-statement
您的if selection1 == 0 {
let milimeter = Milimeter()
userAnswer.text = milimeter.toCnverter(textboxValue!, item: selection2)
}
看起来像这样:
Access Control
Here是关于swift中linq.js
的更多信息
答案 1 :(得分:0)
如果您在.h文件中创建了IBOutlet
标签,请执行以下操作:
@property (nonatomic, strong) IBOutlet UILabel *userAnswer;
尝试通过self.userAnswer