我一直在搜索最近兩天來解決此問題。那我有問題的代碼部分是:[say practiceLabelVar gets the value optional(Goldfish)]
iOS標籤不會更新字符串值的文本,但打印字符串在Swift中正常工作
class PracticeTyping: UIViewController {
@IBOutlet weak var practiceLabel: UILabel!
var practiceLabelVar: String?
var x = "test"
override func viewDidLoad() {
super.viewDidLoad()
if let newPracticeLabelVar = practiceLabelVar {
self.practiceLabel.text = newPracticeLabelVar
print ("\(newPracticeLabelVar)")
x = newPracticeLabelVar
} else {
print ("\(x)")
}
}
}
我得到的輸出是在控制檯如下:
test
Goldfish
注:代碼打印在別的價值語句然後在if語句中打印代碼。
practiceLabel.text
沒有改變並保持其默認值。
請幫忙。 感謝
你在哪裏分配「金魚」練習標籤變量?這是因爲你沒有給練習變量變量賦予任何值。 – dRAGONAIR
我在另一個視圖中從表視圖控制器分配值金魚。當我點擊一個按鈕@IBAction FUNC practiceTyping(_發件人:任意){ performSegue(withIdentifier: 「practiceTypingSegue」,發件人:個體經營) 讓PracticeTypingVar:PracticeTyping = self.storyboard .instantiateViewController(withIdentifier:「PracticeTypingView 「)as! PracticeTyping PracticeTypingVar.practiceLabelVar = myFavoriteAnimals [(發送者作爲AnyObject).TAG]。名稱 self.present(PracticeTypingVar,動畫:真,完成:無) } – Amr