如何在Xcode中实现加载微调器

如何在Xcode中实现加载微调器,xcode,spinner,loading,Xcode,Spinner,Loading,我不熟悉Xcode。有人能帮我实现一个加载微调器吗 1) 我需要从网页加载的开始到结束运行一个加载微调器 2) 此外,我需要在网站/互联网关闭的情况下显示错误警报 import UIKit import WebKit class WebView : WKWebView { /** Initialize the WKWebView. */ init(){ let webConfig:WKWebViewConfiguration = WKWebViewConfiguration()

我不熟悉Xcode。有人能帮我实现一个加载微调器吗

1) 我需要从网页加载的开始到结束运行一个加载微调器

2) 此外,我需要在网站/互联网关闭的情况下显示错误警报

import UIKit
import WebKit
class WebView : WKWebView {

/**
 Initialize the WKWebView.
 */
init(){
    let webConfig:WKWebViewConfiguration = WKWebViewConfiguration()
    super.init(frame:CGRectZero,configuration:webConfig)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.allowsBackForwardNavigationGestures = true
    createHomePage()
}

/**
 Set the position for the WKWebView.
 */
func setPosition(view: UIView) {
    self.translatesAutoresizingMaskIntoConstraints = false;
    let height = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: +0)
    let width = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
    let top = NSLayoutConstraint(item:self,attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 30)
    view.addConstraints([height, width, top])

}

/**
 Set the url the webview should display.
 */
func setUrl(url:String!) {
    if url != nil {
        let url = NSURL(string:url)
        let request = NSURLRequest(URL:url!)
        self.loadRequest(request)
    }
}

/**
 Create the home page.
 */
func createHomePage() {
    let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString
    self.setUrl("https://mywebsite.com?deviceId="+UUID!)

}

}
//另一个swift文件

import UIKit
import WebKit

class ViewController: UIViewController{

var webView: WebView

required init(coder aDecoder: NSCoder) {
    self.webView = WebView()
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(webView)
    webView.setPosition(view)
    view.backgroundColor = (UIColor(red: 0, green: 150/255, blue: 136/255, alpha: 1));
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: Actions

}

在情节提要文件中,在场景中所需的位置添加UIActivityIndicator。然后,转到UIActivityIndicator的属性检查器,并将“停止检查时隐藏”设置为“已检查”或(true)。像这样

然后,创建一个IBOutlet,将UIActivityIndicator连接到ViewController.swift类。可以通过按住Ctrl键并单击UIActivityIndicator并将该行拖动到ViewController.swift类来完成此操作。请注意,下面的拖放图片不是连接UIActivityIndicator,而是连接UITextField。这是因为我刚刚在网上找到了一张图片作为例子,因为我不知道如何在按住Ctrl键的同时拖动Gyazo屏幕截图

然后,您将得到如下提示

您可以随意命名它,但对于这个示例,我将把它命名为“activityIndicator”。然后,它将在链接项目时指向的行上编写此代码

从那里开始,你想使用这些线

activityIndicator.startAnimating()

在需要启动和停止ActivityIndicator以显示WebView正在加载以及加载完成的动画的位置。根据您提供的代码,我认为您应该将ViewController.swift文件更改为:

import UIKit
import WebKit

class ViewController: UIViewController{

var webView: WebView
@IBOutlet var activityIndicator: UIActivityIndicatorView!

required init(coder aDecoder: NSCoder) { 
    //ViewController has been initialized and will be initializing WebView, so start animating the activityIndicator

    self.activityIndicator.startAnimating()
   

    self.webView = WebView()
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(webView)
    webView.setPosition(view)
    view.backgroundColor = (UIColor(red: 0, green: 150/255, blue: 136/255,     alpha: 1));

    //When the webView is added successfully to the subview, I believe that it has loaded correctly and the activityIndicator should stop animating.
    activityIndicator.stopAnimating()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: Actions

}
至于第二个问题,您希望在出现错误时发出警报,我没有WebKit方面的经验,我也不知道。但是,在我做一些研究并找出答案的时候,也许其他人可以帮助他

如果这太深奥了,我的错。您说过您是编写代码的新手,而我从未被教过如何将我的故事板文件中的元素链接到我的ViewController,因此,如果您遇到/曾经遇到过该问题,我希望这将对您有所帮助

import UIKit
import WebKit

class ViewController: UIViewController{

var webView: WebView
@IBOutlet var activityIndicator: UIActivityIndicatorView!

required init(coder aDecoder: NSCoder) { 
    //ViewController has been initialized and will be initializing WebView, so start animating the activityIndicator

    self.activityIndicator.startAnimating()
   

    self.webView = WebView()
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(webView)
    webView.setPosition(view)
    view.backgroundColor = (UIColor(red: 0, green: 150/255, blue: 136/255,     alpha: 1));

    //When the webView is added successfully to the subview, I believe that it has loaded correctly and the activityIndicator should stop animating.
    activityIndicator.stopAnimating()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: Actions

}