UI带有 UIActivity 指示器的警报视图 - 仅显示 Activity 一次

UIAlert View With UIActivity Indicator - Only Shows Activity Once

我有显示警报视图的功能 UIActivityIndictor

var alert = UIAlertView()

Class AlertWithSpinner: NSObject {

    class func alertSpinnerWithTitle (titleText: String, messageText: String){

        alert.title = titleText
        alert.message = messageText

        var alertSubView:UIView = UIView(frame: CGRectMake(83,-10,100,40))
        var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
        loadingIndicator.center = alertSubView.center
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();
        alertSubView.addSubview(loadingIndicator)
        alert.setValue(alertSubView, forKey: "accessoryView")
        loadingIndicator.startAnimating()

        alert.show()

    }

   class func dismiss(){

        alert.dismissWithClickedButtonIndex(-1, animated: true)

    }
 }

警报的第一个显示没问题。所有后续演示文稿都没有显示 UIActivityIndicator,仅显示 UIAlert

您应该创建自己的提醒以备修改。来自 Apple Docs

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

您正在重复使用 UIAlertView,因此第一个 UIActivityIndi​​catorView 仍然是警报的子视图。 添加以下行以在设置 UIAlertView 的消息后立即删除旧的 UIActivityIndi​​catorView。

if alert.subviews.count > 0 {
   alert.subviews[0].removeFromSuperview()
}

我认为实现此目的的最佳方法 - 使用具有类似功能的外部库:

SVProgressHUD

MBProgressHUD