这有点奇怪,我不确定从哪里开始调试。我在标准视图上有一个UILabel,它会根据某些条件更新文本。在IB中,我设置了默认文本,该文本读取为“ Loading ...”,然后viewDidAppear方法根据条件更新了文本。效果很好,但是,如果我随后旋转iPhone(或模拟器),它将UILabel恢复为“正在加载...”的标准文本。
有趣的是,当我在iPad,模拟器和实际设备上查看它时,它都不会将文本更改回默认值,并且可以按照我的期望进行操作。
我尝试检测方向变化并重置文本,但这没有效果,有点像标签已锁定为默认状态。
很高兴在必要时提供代码,但是我真的不确定什么代码是相关的,因为它是简单明了的标签并正在更新其文本。
谢谢
import UIKit
class PredictionViewController: UIViewController {
var predictionData: Predictions!
var embeddedVC: PredictionsTableViewController?
@IBOutlet weak var messageTextBox: UILabel!
@IBOutlet weak var predictionSubmitButton: UIButton!
@IBOutlet weak var predictionSubmitButtonHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//self.messageTextBox.isEditable = false
NotificationCenter.default.addObserver(self, selector: #selector(settingChanged(notification:)), name: UserDefaults.didChangeNotification, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
let preferences = UserDefaults.standard
if (preferences.object(forKey: "regID") == nil)
{
loadLoginScreen()
}
else {
let sv = UIViewController.displaySpinner(onView: self.view)
let predictionStatus = preferences.object(forKey: "predictionStatus") as! String
switch (predictionStatus) {
case "inplay":
setInplay(view: self)
case "finished":
setFinished(view: self)
case "predict":
setPredict(view: self)
default:
self.messageTextBox.text = "Error!"
}
if (self.messageTextBox.isHidden) {
self.messageTextBox.isHidden = false
}
UIViewController.removeSpinner(spinner: sv)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "predictionSegue") {
if let vc = segue.destination as? PredictionsTableViewController {
// get a reference to the embedded VC
self.embeddedVC = vc
}
}
}
@objc func settingChanged(notification: NSNotification) {
let preferences = UserDefaults.standard
let predictionStatus = preferences.object(forKey: "predictionStatus") as! String
switch (predictionStatus) {
case "inplay":
setInplay(view: self)
case "finished":
setFinished(view: self)
case "predict":
setPredict(view: self)
default:
messageTextBox.text = "Error!"
}
}
func setInplay(view: PredictionViewController) {
view.messageTextBox.text = "In Play!"
view.predictionSubmitButtonHeight.constant = 0
}
func setFinished(view: PredictionViewController) {
view.messageTextBox.text = "Finished!"
view.predictionSubmitButtonHeight.constant = 0
}
func setPredict(view: PredictionViewController) {
view.messageTextBox.text = "Predict Now!"
view.predictionSubmitButton.isEnabled = true
view.predictionSubmitButton.setTitle("Submit", for: .normal)
view.predictionSubmitButtonHeight.constant = 58
}
@IBAction func predictionSubmitButtonAction(_ sender: UIButton) {
let preferences = UserDefaults.standard
let sv = UIViewController.displaySpinner(onView: self.view)
CheckTime(finished: { isSuccess in
switch (isSuccess) {
case "inplay":
preferences.set("inplay", forKey: "predictionStatus")
//too late alert
case "finished":
preferences.set("finished", forKey: "predictionStatus")
//too late alert
case "predict":
preferences.set("predict", forKey: "predictionStatus")
if let predictionData = self.embeddedVC?.getPredictionData() {
//send back to website
let regID = preferences.object(forKey: "regID")
let url = URL(string: "[URL]")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let bodyData = "{}"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let data = data, let _ = response, error == nil else
{
DispatchQueue.main.async(
execute: {
UIViewController.removeSpinner(spinner: sv)
self.displayAlertMessage(message: "response error: \(String(describing: error?.localizedDescription))", type: "error")
}
)
return
}
do {
let decoder = JSONDecoder()
let predictionResult = try decoder.decode(ReturnData.self, from: data)
DispatchQueue.main.async(
execute: {
if (predictionResult.success) {
self.displayAlertMessage(message: predictionResult.message, type: "message", title: "Predictions Received")
}
else {
self.displayAlertMessage(message: "response error: \(String(describing: error?.localizedDescription))", type: "error")
}
UIViewController.removeSpinner(spinner: sv)
}
)
} catch {
DispatchQueue.main.async(
execute: {
UIViewController.removeSpinner(spinner: sv)
self.displayAlertMessage(message: "response error: \(error)", type: "error")
}
)
return
}
})
task.resume()
}
default:
UIViewController.removeSpinner(spinner: sv)
self.messageTextBox.text = "Error!"
preferences.set("error", forKey: "predictionStatus")
}
preferences.synchronize()
if (self.messageTextBox.isHidden) {
self.messageTextBox.isHidden = false
}
})
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
//imageView.image = UIImage(named: const2)
} else {
print("Portrait")
//imageView.image = UIImage(named: const)
}
self.messageTextBox.text = "Error!"
}
答案 0 :(得分:1)
您可以使用此Delegate方法进行屏幕定向吗?
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
}, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
//refresh view once rotation is completed not in will transition as it returns incorrect frame size.Refresh here
**//---> Set the text for label here.**
})
super.viewWillTransition(to: size, with: coordinator)
}
答案 1 :(得分:1)
我认为您应该将代码从viewDidAppear
中删除,并放入viewDidLoad
中。
如果您不想以其他方向使用代码,则应取消选中所有其他方向,而仅选择要实施的代码,这样可以解决您的问题,但是如果您想以其他方向使用,尝试按照我说的做,看看是否行得通。
即使我刚才所说的都不起作用,如果您有条件在发生过渡时更改文本,请尝试浏览一下代码。
还有一点,只是一个提示,请避免在一个简单的动作中放入太多代码,尝试在其他方法中进行重构,然后在您的动作中调用它。