如何使用alertcontroller添加警报消息?

时间:2018-11-28 09:53:59

标签: ios swift uialertcontroller

我正在使用Xcode 9.3.1 Swift 4开发开源应用程序。 有一个播放按钮。当用户单击播放按钮时,音频文件会在开始播放之前自动下载。我想添加一条警告消息,警告用户,显示文件大小,并允许他接受或不进行下载。

这是我的audiobarview.xib代码:

import GenericDataSources
import QueuePlayer
import UIKit

internal protocol AdvancedAudioOptionsViewControllerDelegate : AnyObject {
    internal func advancedAudioOptionsViewController(_ controller: AdvancedAudioOptionsViewController, finishedWith options: AdvancedAudioOptions)
}

internal class AdvancedAudioOptionsViewController : UIViewController, UIGestureRecognizerDelegate {
    weak internal var delegate: AdvancedAudioOptionsViewControllerDelegate?

    @IBOutlet weak internal var tableView: UITableView!
    @IBOutlet weak internal var contentView: UIView!
    @IBOutlet weak internal var bottomConstraint: NSLayoutConstraint!
    @IBOutlet weak internal var navigationBar: UINavigationBar!

    lazy internal var playButton: UIButton { get set }

    internal init(options: AdvancedAudioOptions)

    required internal init?(coder aDecoder: NSCoder)

    override internal func viewDidLoad()
    override internal func viewDidAppear(_ animated: Bool)
    override internal func viewDidLayoutSubviews()

    @IBAction internal func playButtonTapped(_ sender: Any)
    @IBAction internal func dismissView(_ sender: Any)

    internal func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
}

extension UIView {
    internal func round(corners: UIRectCorner, radius: CGFloat)
}

这是audiofilesdownloader.swift代码:

import BatchDownloader
import PromiseKit

class AudioFilesDownloader {
    let audioFileList: QariAudioFileListRetrieval
    let downloader: DownloadManager
    let ayahDownloader: AnyInteractor<AyahsAudioDownloadRequest, DownloadBatchResponse>

    private var response: DownloadBatchResponse?

    init(audioFileList: QariAudioFileListRetrieval,
         downloader: DownloadManager,
         ayahDownloader: AnyInteractor<AyahsAudioDownloadRequest, DownloadBatchResponse>) {
        self.audioFileList  = audioFileList
        self.downloader     = downloader
        self.ayahDownloader = ayahDownloader
    }

    func cancel() {
        response?.cancel()
        response = nil
    }

    func needsToDownloadFiles(qari: Qari, range: VerseRange) -> Bool {
        let files = filesForQari(qari, range: range)

        return !files.filter { !FileManager.documentsURL.appendingPathComponent($0.destinationPath).isReachable }.isEmpty
    }

    func getCurrentDownloadResponse() -> Promise<DownloadBatchResponse?> {
        if let response = response {
            return Promise(value: response)
        } else {
            return downloader.getOnGoingDownloads().then { batches -> DownloadBatchResponse? in
                let downloading = batches.first { $0.isAudio }

                self.createRequestWithDownloads(downloading)

                return self.response
            }
        }
    }

    func download(qari: Qari, range: VerseRange) -> Promise<DownloadBatchResponse?> {
        return ayahDownloader
            .execute(AyahsAudioDownloadRequest(range: range, qari: qari))
            .then(on: .main) { responses -> DownloadBatchResponse? in
                // wrap the requests
                self.createRequestWithDownloads(responses)

                return self.response
            }
    }

    private func createRequestWithDownloads(_ batch: DownloadBatchResponse?) {
        guard let batch = batch else { return }

        response = batch
        response?.promise.always { [weak self] in
            self?.response = nil
        }
    }

    func filesForQari(_ qari: Qari, range: VerseRange) -> [DownloadRequest] {
        return audioFileList.get(for: qari, range: range).map {
            DownloadRequest(url: $0.remote, resumePath: $0.local.stringByAppendingPath(Files.downloadResumeDataExtension), destinationPath: $0.local)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

假设您将文件大小设置为变量filesize

快捷键4:

let alert = UIAlertController(title: "Proceed with download?", message: "File size: \(filesize)", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in
            // Leave empty, dismisses the alert
        }))

        alert.addAction(UIAlertAction(title: "Download Song", style: .default, handler: {(alert: UIAlertAction!) in
            // Code to download song
        }))

        self.present(alert, animated: true, completion: nil)