I am writing my first iOS
app using Swift
in which, I have a UIButton
which has Start/Pause functionality. On tapping Start, it will load a image in UIWebView
and change text to Pause. When tapped on Pause, it will stop loading the webView
.
I am using the following approach
@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)
}
}
But, this code is changing the text from start to Pause and vice-versa. I am unable to load the resources on webView
Upon executing below code,
@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)
}
*/
}
I have observed, that on second tap of the button, the view is loaded in webView
. How do I overcome this second tab event make change it to first tab and achieve this functionality ?
EDIT 1
On Executing
@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)
}*/
}
The following is the output
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")