我有一个webView
,我需要显示需要更改的单个按钮的刷新/停止。当用户点击刷新时,按钮需要显示停止图标,直到webView
完成加载并在加载视图后显示返回刷新图标。我需要和方法来创建它。
答案 0 :(得分:1)
有几种不同的方法可以做到这一点。您可以在ViewController中声明另一个名为“allowsRefresh”的变量,然后当您单击该按钮时,您可以执行类似
的操作@IBAction refreshOrStop(sender: AnyObject) {
if (allowsRefresh) {
// Do stuff that refreshes the webpage
} else {
// Do stuff that stops the refresh
}
allowsRefresh = !allowsRefresh
// Inverse itself. If it was true, change it false. If it was false, change it to true
}
肯定有比allowRefresh更好的名字,但你明白了。
答案 1 :(得分:0)
你可以尝试这样的事情首先声明一个像这样的全局按钮
var button: UIButton = UIButton()
现在创建一个这样的函数
func setRefreshItem() {
button.frame = CGRectMake(0, 0, 18, 25)
//Set backgroundImage in button for both state Normal and selected
button.setBackgroundImage(UIImage(named: "Refresh"), forState: UIControlState.Normal)
button.setBackgroundImage(UIImage(named: "Stop"), forState: UIControlState.Selected)
button.addTarget(self, action: #selector(self.refreshWebView(_:))",forControlEvents: UIControlEvents.TouchUpInside)
let rightItem:UIBarButtonItem = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = rightItem
}
现在在refreshWebView
viewController
方法
func refreshWebView(sender: UIButton) {
if (sender.selected) {
self.webView.stopLoading()
}
else {
self.webView.reload()
}
sender.selected = !sender.selected
}
//Delegate method of webView
func webViewDidFinishLoad(webView : UIWebView) {
self.buttton.selected = false
}
同时在setRefreshItem
viewDidLoad