在 UIButton 上实现 start/pause touch up inside

Implement start/pause on UIButton touch up inside

我正在使用 Swift 编写我的第一个 iOS 应用程序,其中,我有一个具有 Start/Pause 功能的 UIButton。点击开始时,它将加载 UIWebView 中的图像并将文本更改为暂停。点击暂停时,它将停止加载 webView.

我正在使用以下方法

@IBOutlet weak var startPauseButton: UIButton!

    @IBAction func startPauseButtonAction(sender: UIButton) {

         if  startPauseButton!.currentTitle == "Start"{

            if let htmlURL = NSBundle.mainBundle().pathForResource("index", ofType: "html" ){
                //  let htmlURL = NSBundle.mainBundle().URLForResource("index", withExtension: "html")
                let requestURL = NSURL(string: htmlURL)
                 let requestObject = NSURLRequest(URL: requestURL!)
                webView.sizeToFit()
                 webView.loadRequest(requestObject)
            }else{
                let url =  NSBundle.mainBundle().URLForResource("video", withExtension: "html")
                 let requestObject = NSURLRequest(URL:url!);
                 webView.loadRequest(requestObject)
            }

            startPauseButton.setTitle("Pause", forState: UIControlState.Normal)
         }else{
            webView.stopLoading()
            startPauseButton.setTitle("Start", forState: UIControlState.Normal)
        }


    }

但是,此代码将文本从开始更改为暂停,反之亦然。我无法加载 webView

上的资源

执行以下代码后,

 @IBOutlet weak var startPauseButton: UIButton!

    @IBAction func startPauseButtonAction(sender: UIButton) {

      //  if  startPauseButton!.currentTitle == "Start"{

            if let htmlURL = NSBundle.mainBundle().pathForResource("index", ofType: "html" ){
                //  let htmlURL = NSBundle.mainBundle().URLForResource("index", withExtension: "html")
                let requestURL = NSURL(string: htmlURL)
                 let requestObject = NSURLRequest(URL: requestURL!)
                webView.sizeToFit()
                 webView.loadRequest(requestObject)
            }else{
                let url =  NSBundle.mainBundle().URLForResource("video", withExtension: "html")
                 let requestObject = NSURLRequest(URL:url!);
                 webView.loadRequest(requestObject)
            }

            startPauseButton.setTitle("Pause", forState: UIControlState.Normal)
     /*   }else{
            webView.stopLoading()
            startPauseButton.setTitle("Start", forState: UIControlState.Normal)
        }
*/

    }

我观察到,第二次点击按钮时,视图加载到 webView。我如何克服第二个选项卡事件将其更改为第一个选项卡并实现此功能?

编辑 1

执行时

 @IBAction func startPauseButtonAction(sender: UIButton) {
        println("button has been tapped \(startPauseButton!.titleForState(.Normal))")
     //   if startPauseButton!.titleForState(.Normal) == "Start" {
            if let htmlURL = NSBundle.mainBundle().URLForResource("index", withExtension: "html") {
                let requestObject = NSURLRequest(URL: htmlURL)
                webView.sizeToFit()
                webView.loadRequest(requestObject)
            }else{
                let url =  NSBundle.mainBundle().URLForResource("video", withExtension: "html")
                let requestObject = NSURLRequest(URL:url!);
                webView.loadRequest(requestObject)
            }

            startPauseButton.setTitle("Pause", forState: UIControlState.Normal)
    /*    }else{
            webView.stopLoading()
            startPauseButton.setTitle("Start", forState: UIControlState.Normal)
        }*/
    }

下面是输出 button has been tapped Optional("Start") button has been tapped Optional("Pause")//here the webview is getting loaded button has been tapped Optional("Pause") button has been tapped Optional("Pause")

尝试按照此 Whosebug 线程中的说明加载 webview -

How to load URL in UIWebView in Swift?