我有一个莫名其妙的情况,我将变量passageText设置为某个值,但它不会更新UITextView。
这是我的控制器代码:
import UIKit
import CoreData
class FavoritesDetailsViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var referenceLabel: UILabel!
//I've confirmed that the UITextView is connected to this outlet
@IBOutlet weak var passageText: UITextView!
var specifiedFavorite : String = ""
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
referenceLabel.text? = specifiedFavorite
print("The FavoritesDetailsViewController viewDidLoad")
print("The Value of variable specifiedFavorite sent from segue is: \(specifiedFavorite)")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Passages")
request.predicate = NSPredicate(format: "reference == %@", specifiedFavorite)
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request)
print("fetch request was successful")
if results.count > 0 {
print("The number of results returned is: \(results.count)")
for result in results as! [NSManagedObject] {
print("We got results!")
if let returnedText = result.value(forKey: "passagetext") as? String {
self.passageText.text! = returnedText
print("This is the text from the passageText textview: \(self.passageText.text!)")
}
}
}
} catch {
print("Couldn't fetch results")
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("The FavoritesDetailsViewController viewDidLoad")
print("The Value of variable specifiedFavorite sent from segue is: \(specifiedFavorite)")
print(specifiedFavorite)
}
}
以下是我的日志输出:
The Value of variable specifiedFavorite sent from segue is: Genesis 3:1
fetch request was successful
The number of results returned is: 1
We got results!
This is the text from the passageText textview: The Fall
Now the serpent was more crafty than any other beast....
所以看来UITextView文本名为&#34; passageText&#34;已经设置得当。
self.passageText.text! = returnedText
我知道无法添加.text通常是一个新手错误,我想我在这里避免了。我还确认UITextView已连接到我的IBOutlet。我不确定为什么用户界面没有更新到新值。我可以用一些新鲜的眼睛。
答案 0 :(得分:0)
好吧,我会把这个白垩体验起来。问题是我的自动布局约束设置不正确,因此UITextView在UI中不可见。这很容易被忽略,因为在Xcode中,textview是可见的。但是不要忽视那些红线和Xcode警告!它们表明即使应用程序编译和运行也会影响正常功能的问题。希望有人从我的错误中学习。