swift2 如何在swift中存储我的第一个文本字段而不要求第二次

iqxoj9l9  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(222)

我创建了一个UITextField,当用户在其中写入链接并输入时,我的文本字段消失,而webView出现。
我想做的是,当用户第一次写入链接时,文本字段会保存该链接,当用户再次打开应用程序时,网页视图会直接从用户在文本字段中写入的最后一个链接打开。基本上,存储的链接应该会在第二次运行。
下面是我的全部代码:

import UIKit
import Foundation

let urlKey = "User URL"

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.hidden = true
            self.textField.addTarget(self, action: #selector(ViewController.textFieldDidUpdate(_:)), forControlEvents: UIControlEvents.EditingChanged)

      if doesURLExist() {
        self.textField.text = getURL()
  }
}
    // Text Field Delegate
    func textFieldDidUpdate(textField: UITextField)
    {
        // Remove Spaces
        textField.text = textField.text!.stringByReplacingOccurrencesOfString(" ", withString: "", options: [], range: nil)

        // Validate URL
        NSURL.validateUrl(textField.text, completion: { (success, urlString, error) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if (success)
                {
                  self.saveURL(urlString!)
                  self.webView.hidden = false
                  self.textField.hidden = true 
                  let request = NSURLRequest(URL: NSURL(string: urlString!)!)
                  self.webView.loadRequest(request)
                }
                else
                {
                    self.webView.stopLoading()
                    self.webView.hidden = true
                }
            })
        })

          }
    @IBAction func dismissKeyboard(sender: AnyObject) {
        self.resignFirstResponder()
        self.view.endEditing(true)
    }
  func saveURL(urlString: String)  {
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(urlString, forKey: urlKey)
  }

  func getURL() -> String {
    let defaults = NSUserDefaults.standardUserDefaults()
    let urlString = defaults.objectForKey(urlKey) as! String
    return urlString
  }

  func doesURLExist() -> Bool {
    let defaults = NSUserDefaults.standardUserDefaults()
    guard let _ = defaults.objectForKey(urlKey) where defaults.objectForKey(urlKey) is String else {
      return false
    }
    return true
  }
}

下面是我在GitHub中的项目:https://github.com/anappleapp/NSURLvalidation

vom3gejh

vom3gejh1#

首先,你需要通过调用doesURLExist来检查url是否存在,如果存在,你选择不显示该文本域,如果不存在,调用saveURLNSUserDefaults提供了一种简单的方法来存储轻量级数据。

let urlKey = "User URL"     

func saveURL(urlString: String)  {
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(urlString, forKey: urlKey)
}

func getURL() -> String {
    let defaults = NSUserDefaults.standardUserDefaults()
    let urlString = defaults.objectForKey(urlKey) as! String
    return urlString
}

func doesURLExist() -> Bool {
    let defaults = NSUserDefaults.standardUserDefaults()
    guard let _ = defaults.objectForKey(urlKey) where defaults.objectForKey(urlKey) is String else {
        return false
    }
    return true
}

因此,您的类应该类似于:

import UIKit
import Foundation

let urlKey = "User URL"     

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.hidden = true

        self.textField.addTarget(self, action: #selector(ViewController.textFieldDidUpdate(_:)), forControlEvents: UIControlEvents.EditingChanged)

        if(doesURLExist) {
            self.textField.text = getURL()
        }
        // Demo UI Settings
    }
}

  // Text Field Delegate
func textFieldDidUpdate(textField: UITextField)
{
    // Remove Spaces
    textField.text = textField.text!.stringByReplacingOccurrencesOfString(" ", withString: "", options: [], range: nil)

    // Validate URL
    NSURL.validateUrl(textField.text, completion: { (success, urlString, error) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            if (success) {
              self.saveURL(urlString)
              self.webView.hidden = false
              self.textField.hidden = true 
              let request = NSURLRequest(URL: NSURL(string: urlString!)!)
              self.webView.loadRequest(request)
            } else {
                self.webView.stopLoading()
                self.webView.hidden = true
            }
        })
    })
}

不要忘记将原始函数添加到类中。

qacovj5a

qacovj5a2#

您应该将输入的字符串保存为用户默认设置。当您的应用打开时,您应该检查用户默认设置,以查看是否已经保存了字符串。
Swift 2代码将您的URL保存为用户默认值:

NSUserDefaults.standardUserDefaults().setObject(urlString!, forKey: "EnteredURLString")

Swift 2代码检查是否有保存的URL字符串:

if let urlString = NSUserDefaults.standardUserDefaults().stringForKey("EnteredURLString") {
}

相关问题