gpt4 book ai didi

ios - UILabel 在 iPhone 旋转时恢复为默认文本

转载 作者:行者123 更新时间:2023-11-28 11:47:08 25 4
gpt4 key购买 nike

这是一个有点奇怪的问题,我不确定从哪里开始调试。我在标准 View 上有一个 UILabel,我根据特定条件更新文本。在 IB 中,我设置了默认文本“正在加载...”,然后 viewDidAppear 方法根据条件更新文本。这工作正常,但是,如果我随后旋转我的 iPhone(或模拟器),它会将 UILabel 恢复为“正在加载...”的标准文本。

有趣的是,当我在 iPad 上查看它时,无论是模拟器还是实际设备,它都没有将文本更改回默认值,而是按照我的预期运行。

我已经尝试检测方向变化并重置文本,但这没有任何效果,这有点像标签已锁定为默认状态。

很乐意在必要时提供代码,但我真的不确定什么代码是相关的,因为它是一个直接的标签并更新了它的文本。

谢谢

Portrait View

Landscape View

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!"
}

最佳答案

你能使用这个 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)
}

关于ios - UILabel 在 iPhone 旋转时恢复为默认文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52441750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com