Swift 在函数中快速添加子视图
我试图在按下按钮时显示加载子视图。我不知道为什么,但子视图仅在显示警报视图时出现。我希望在功能开始时立即显示加载屏幕 这是什么原因造成的?我将如何纠正Swift 在函数中快速添加子视图,swift,subview,Swift,Subview,我试图在按下按钮时显示加载子视图。我不知道为什么,但子视图仅在显示警报视图时出现。我希望在功能开始时立即显示加载屏幕 这是什么原因造成的?我将如何纠正 @IBAction func registerManual(_ sender: Any) { // Check the validity of the inputs let isValid = manualRegistration().validate(firstName: "\(String(describi
@IBAction func registerManual(_ sender: Any) {
// Check the validity of the inputs
let isValid = manualRegistration().validate(firstName: "\(String(describing: firstName.text!))", lastName: "\(String(describing: lastName.text!))", email: "\(String(describing: email.text!))", password: "\(String(describing: password.text!))", dob: dobSelected)
if(isValid == "success"){
let loadingScreen = xibLoad().loadingScreen()
loadingScreen.frame.size.height = view.frame.height
loadingScreen.tag = 100
view.addSubview(loadingScreen)
// Submit to backend.
let params = "firstname=\(escpae().url(string: firstName.text!))&lastname=\(escpae().url(string:lastName.text!))&email=\(escpae().url(string:email.text!))&dob=\(escpae().url(string: selectedDateText.text!))&password=\(escpae().url(string:password.text!))¤cy=\(currency)&language=\(language)®ion=\(region)"
let resultArray = serverCall().post(endPoint: "registermanual.php?", params: "\(params)", fromViewController: self)
if(resultArray.count > 0 && resultArray[0]["error"] == nil){
self.defaults.set("\(resultArray[0]["token"]!)", forKey: "token")
DispatchQueue.main.async {
self.performSegue(withIdentifier:"goLoading", sender: self)
}
}
if(resultArray.count > 0 && resultArray[0]["error"] != nil){
alert().show(title: "ERROR", message: "\(resultArray[0]["error"]!)", button: "OK", viewc: self)
}
}else{
alert().show(title: "Oops!", message: "\(isValid)", button: "OK", viewc: self)
}
}
加载屏幕:
class loadingScreen: UIView {
@IBOutlet weak var gifView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
//custom logic goes here
gifView.loadGif(name: "loading")
}
}
您可以尝试这样做,即使您将子视图添加到if语句之外,也只需毫秒(取决于验证时间),但它将阻止任何用户事件
@IBAction func registerManual(_ sender: Any) {
loadingScreen.frame.size.height = view.frame.height
loadingScreen.tag = 100
view.addSubview(loadingScreen)
// view.layoutSubviews() don't call it directly
// Check the validity of the inputs
let isValid = manualRegistration().validate(firstName: "\(String(describing: firstName.text!))", lastName: "\(String(describing: lastName.text!))", email: "\(String(describing: email.text!))", password: "\(String(describing: password.text!))", dob: dobSelected)
if(isValid == "success"){
请显示加载屏幕的代码。它似乎没有宽度值?它是手动注册api调用或本地验证??手动注册是本地调用hi@Sh_Khan,不幸的是,这并不能解决问题。有趣的是,如果我删除API调用'resultArray',加载屏幕就会像我预期的那样出现。我怀疑这可能与此有关,但我假设子视图会在该函数启动之前出现。您在主线程中调用它的位置是这样的,请在后台线程中调用它,因为它会影响主线程的其他任务,特别是添加加载视图HI,这已经解决了问题!非常感谢你!