我在stackoverflow中经历了类似的问题,但是没有一种解决方案对我有用。
我正在执行网络呼叫,并且根据呼叫的响应,我将执行导航。为了获得适当的用户界面,我需要显示一个加载对话框以向用户显示正在发生的事情。因为我的加载对话框将被使用几次,所以我创建了一个像这样的扩展
>>> import numpy as np
>>> x = np.load('mnist.npz', mmap_mode='r')
>>> for k in x.files:
... print(k)
...
x_test
x_train
y_train
y_test
>>>
在执行网络调用之前,我正在调用showLoadingDialog()方法,当我收到响应时,我在调用dismissLoader()方法来隐藏加载对话框,然后根据响应执行如下导航
这是我的控制器正在实施的查看协议
import UIKit
extension UIViewController {
func showLoadingDialog() {
var alertController: UIAlertController!
alertController = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
//alertController.view.tintColor = brownColor //UIColor.brown
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
loadingIndicator.color = UIColor.white
loadingIndicator.startAnimating()
alertController.view.addSubview(loadingIndicator)
alertController.view.subviews.first?.backgroundColor = UIColor.clear
present(alertController, animated: false, completion: nil)
}
func dismissLoader() {
dismiss(animated: false, completion: nil)
}
func showToast(controller: UIViewController, message : String, seconds: Double) {
let toastLabel = UILabel(frame: CGRect(x: 10, y: self.view.frame.size.height-100, width: self.view.frame.size.width-20, height: 50))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = message
toastLabel.numberOfLines = 2
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 3, delay: 1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
}
这是我的控制器...
protocol ShipmentDetailsView: class {
func setShipmentResponse(shipmentResponse: ShipmentResponse)
func showErrorMessage(message: String)
func showProgressDialog()
func hideProgressDialog()
func getShipmentId() -> String
func navigateToLogin()
func onStatusCheckSuccess(shipmentResponse: ShipmentResponse)
}
这是我的演示者代码:
import UIKit
class ShipmentDetailsViewController: UIViewController, ShipmentDetailsView{
@IBOutlet weak var shipmentNumber: UILabel!
@IBOutlet weak var siteName: UILabel!
@IBOutlet weak var currentDestination: UILabel!
@IBOutlet weak var address: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var phoneNumber: UILabel!
@IBOutlet weak var startShipmentButton: UIButton!
var viewController: UIViewController!
var shipmentId: String = ""
var shipmentResponse: ShipmentResponse!
var presenter: IShipmentDetailsPresenter!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hideKeyboard()
}
private func onCreate() {
presenter = ShipmentDetailsPresenter(view: self)
}
override func viewDidLoad() {
super.viewDidLoad()
onCreate()
}
@IBAction func startShipment(_ sender: Any) {
presenter.getShipmentByIdToCheckStatus(shipmentId: self.shipmentId)
}
public func setShipmentId(shipmentId: String) {
self.shipmentId = shipmentId
}
func navigateToLogin() {
//Todo
}
func onStatusCheckSuccess(shipmentResponse: ShipmentResponse) {
if shipmentResponse.Status == 0 {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let verifyViewController = storyboard.instantiateViewController(withIdentifier: "RunningShipmentViewController") as! RunningShipmentViewController
self.navigationController?.pushViewController(verifyViewController, animated: true)
} else {
showToast(controller: self, message: "Cannot start this shipment", seconds: 0.5)
}
}
func getShipmentId() -> String {
return self.shipmentId
}
func showErrorMessage(message: String) {
showToast(controller: self, message: message, seconds: 1.0)
}
func showProgressDialog() {
showLoadingDialog()
}
func hideProgressDialog() {
dismissLoader()
}
func setShipmentResponse(shipmentResponse: ShipmentResponse) {
self.shipmentResponse = shipmentResponse
updateUI(shipmentResponse: shipmentResponse)
}
private func updateUI(shipmentResponse: ShipmentResponse) {
if shipmentResponse.ShipmentNumber != "" {
self.shipmentNumber.text = shipmentResponse.ShipmentNumber
} else {
self.shipmentNumber.text = "-"
}
if shipmentResponse.SiteName != "" {
self.siteName.text = shipmentResponse.SiteName
} else {
self.siteName.text = "-"
}
if shipmentResponse.DestinationName != "" {
self.currentDestination.text = shipmentResponse.DestinationName
} else {
self.currentDestination.text = "-"
}
if shipmentResponse.SiteAddress != "" {
self.address.text = shipmentResponse.SiteAddress
} else {
self.address.text = "-"
}
if shipmentResponse.OrgResponsiblePersonName != "" {
self.name.text = shipmentResponse.OrgResponsiblePersonName
} else {
self.name.text = "-"
}
if shipmentResponse.OrgResponsiblePersonPhone != "" {
self.phoneNumber.text = shipmentResponse.OrgResponsiblePersonPhone
} else {
self.phoneNumber.text = "-"
}
}
}
这是我的交互者:
class ShipmentDetailsPresenter : IShipmentDetailsPresenter, ILoadShipmentInteractorOutput {
var view: ShipmentDetailsView!
var loadShipmentInteractor : ILoadShipmentInteractor!
init(view: ShipmentDetailsView) {
self.view = view
}
func getShipmentByIdToCheckStatus(shipmentId: String) {
view.showProgressDialog()
let queryString = NSString(format:Queries.SELECT_SHIPMENT_BY_ITEM_ID_QUERY as NSString, shipmentId)
loadShipmentInteractor = LoadShipmentInteractor(loadShipmentInteractorOutput: self)
loadShipmentInteractor.getShipmentByItemIdToCheckStatus(queryString: queryString as String)
}
func onGetShipmentByItemIdToCheckStatus(response: BaseResponse) {
view.hideProgressDialog()
if response.statusCode == 200 {
if let json = response.response.data(using: .utf8){
guard let obj = try? JSONDecoder().decode(ShipmentsResponse.self, from: json) else {return}
if obj.StatusCode == 0 && obj.TotalRecordCount>0 {
view.onStatusCheckSuccess(shipmentResponse: obj.Results[0])
} else {
//Todo
}
} else {
let errMessage = NSLocalizedString("error_parsing_response", comment: "Message to show if parsing went wrong")
print(errMessage)
}
} else if response.statusCode == 400 {
view.showErrorMessage(message: Messages.SESSION_EXPIRED_MESSAGE)
view.navigateToLogin()
} else {
view.showErrorMessage(message: Messages.SOMETHING_WENT_WRONG_MESSAGE)
//view.navigateToLogin()
}
}
}