iphone - 当两者都以编程方式实现时(Swift),使用 UIButton 重新加载 UIWebView

标签 iphone xcode swift uiwebview uibutton

我有一个加载嵌入 SoundCloud 轨道的 UIWebView。我从网站复制了代码,创建了一个 html 文件,并将其包含在 Xcode 项目中。它加载得很好;但是,我希望用户能够重新加载 WebView,因为 UIWebView 非常小并且没有任何导航按钮。它只是显示 SoundCloud 嵌入小部件。

有没有办法使用界面生成器来实现此目的?点击按钮时应用程序崩溃。

我已经以编程方式在屏幕上实现了UIWebViewUIButton,因为它们的大小和坐标会根据屏幕的大小而有所不同。 iPhone 屏幕。

UIWebView 在 vi​​ewDidLoad 函数之前设置为nil,以便可以在其他函数中访问它(例如reload函数)。

class SecondViewController: UIViewController, UIWebViewDelegate {

    let WebView: UIWebView! = nil
    override  func viewDidLoad() {
        super.viewDidLoad()

        let WebViewDetails = getCoordAndSize("WEBV") // stores 4 values into one variable. its a tuple
        let WebView = UIWebView(frame: CGRectMake(WebViewDetails.0, WebViewDetails.1, WebViewDetails.2, WebViewDetails.3))

        //load local html file
        let localfilePath = NSBundle.mainBundle().URLForResource("sctrack", withExtension: "html")
        let myRequest = NSURLRequest(URL: localfilePath!)
        WebView.loadRequest(myRequest)
        WebView.delegate = self

        let refreshButton = UIButton()
        refreshButton.setTitle("Refresh", forState: .Normal)
        refreshButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        refreshButton.backgroundColor = UIColor.clearColor()
        refreshButton.layer.cornerRadius = 5
        refreshButton.layer.borderWidth = 1
        refreshButton.layer.borderColor = UIColor.whiteColor().CGColor
        refreshButton.frame = CGRectMake((self.view.frame.size.width - 240) / 2, (imageViewDetails.2 - 40),240,40)


        //(LOOK HERE) BUTTON ACTION . What I want it to do is to reload html file when user taps on it
        refreshButton.addTarget(self, action: #selector(reload), forControlEvents: .TouchUpInside)
        self.view.addSubview(refreshButton)
    }

下面是refreshButton的函数。当用户点击按钮时,它会调用重新加载函数。我希望它重新加载本地文件。我将“错误在此”的评论添加到

    func reload(sender: UIButton!) {
        //ERROR HERE
        WebView.reload()

        let alertView = UIAlertController(title: "Message", message: "Track refreshed! Enjoy 🤘🏾✨", preferredStyle: .Alert)
        // Initialize Actions
        let okAction = UIAlertAction(title: "Ok", style: .Default) { (action) -> Void in
        }
        alertView.addAction(okAction)
        self.presentViewController(alertView, animated: true, completion: nil)
    }
}

我尝试将 html 请求包含在重新加载函数中,并使用 WebView.loadRequest(myRequest) 而不是 WebView.reload,但应用程序仍然崩溃。

最佳答案

您没有初始化在类级别声明的WebView。再次在 viewDidLoad 中创建另一个具有相同名称的变量

      let WebView = UIWebView(frame: CGRectMake(WebViewDetails.0, WebViewDetails.1, WebViewDetails.2, WebViewDetails.3))

因此,您在 reload 方法中使用的 WebView 从未初始化,因此出现错误/崩溃。

删除 WebViewviewDidLoad 中的 let 关键字。一切都会顺利进行。

关于iphone - 当两者都以编程方式实现时(Swift),使用 UIButton 重新加载 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004790/

相关文章:

swift - 为什么我无法在操作函数中切换 UIGestureRecognizer 的属性

iphone - 使用键盘后,Xcode 模拟器键盘不出现

iphone - 需要 iPhone 上的风景照片

iphone - mp3 的持续时间返回零

xcode - Xcode 中已存在同名项目

ios - 在 iOS 9.2 中使用企业部署创建 ipa 文件

xcode - Podfile PXGoogleDirections 和 GoogleMaps 名称冲突

Swift/firebase/firebase 中 child 数量的升序

iphone - 使用 Core Motion 计算倾斜角

ios - UIView 在没有 UIViewController 的情况下获取宽度/高度