UIbutton Action方法在swift中不起作用

时间:2015-05-08 06:07:07

标签: swift

我正在创建简单视频下载应用

一切都运行良好,但是当我点击下载按钮视频下载工作完美但活动指示不起作用并在println中写任何消息也无法正常工作

我在自定义视图上添加按钮,我使用SwiftSpinner.swift库作为活动指示器

这是代码

  @IBAction func downloadAction(sender: AnyObject) {
    println("hello")

    SwiftSpinner.show("Downloading...", animated: true)
    let urlPath = self.photoInfo?.sourceImageURL
    let urlLastPath = self.photoInfo?.sourceImageURL.lastPathComponent
    var data : NSData = NSData(contentsOfURL: urlPath!)!

    var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0].stringByAppendingPathComponent(urlLastPath!) as String
    println(path)

    delay(seconds: 16.0, completion: {
        SwiftSpinner.show("Downloading Completed", animated: false)
    })



    data.writeToFile(path, atomically: true)

    delay(seconds: 18.0, completion: {
        SwiftSpinner.hide()
    })

    UISaveVideoAtPathToSavedPhotosAlbum(path, nil, nil, nil)

}

1 个答案:

答案 0 :(得分:0)

这是您完整的工作代码:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func downloadAction(sender: AnyObject) {
        println("hello")

        //Spinner will show when you press button.

        SwiftSpinner.show("Downloading...", animated: true)
        if let checkedUrl = NSURL(string: "http://hdwallpaperd.com/wp-content/uploads/hd-widescreen-wallpapers-4.jpg") {
            downloadImage(checkedUrl)
        }

    }

    func downloadImage(url:NSURL){
        println("Started downloading \"\(url.lastPathComponent!.stringByDeletingPathExtension)\".")
        getDataFromUrl(url) { data in

            // Do that stuff into background queue

            dispatch_async(dispatch_get_main_queue()) {

                SwiftSpinner.show("Downloading Completed", animated: false)

                //Here you can set delay so that you can perform any action after download finish.

                let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
                dispatch_after(delayTime, dispatch_get_main_queue()) {

                    // This code will run after 2 second in this case.

                    var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0].stringByAppendingPathComponent("Image1") as String
                    println(path)
                    data!.writeToFile(path, atomically: true)
                    SwiftSpinner.hide()  // After 2 second your spinner will hide.
                    UISaveVideoAtPathToSavedPhotosAlbum(path, nil, nil, nil)
                }
            }
        }
    }

    func getDataFromUrl(urL:NSURL, completion: ((data: NSData?) -> Void)) {
        NSURLSession.sharedSession().dataTaskWithURL(urL) { (data, response, error) in
            completion(data: NSData(data: data))
            }.resume()
    }
}

您可以参考THIS示例项目。