繁体   English   中英

在内部的UIButton上实现启动/暂停

[英]Implement start/pause on UIButton touch up inside

我正在使用Swift编写我的第一个iOS应用程序,其中有一个具有启动/暂停功能的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")输出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")

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

如何在Swift中的UIWebView中加载URL?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM