美文网首页
swift3.0中UserDefaults的使用

swift3.0中UserDefaults的使用

作者: 骑着雅迪小毛驴上班的老瞿 | 来源:发表于2017-11-30 20:34 被阅读0次

    UserDefaults是一种存轻量级的数据
    //
    // ViewController.swift
    // userDefault
    //
    // Created by quyuanpeng on 17/4/21.
    // Copyright © 2017年 quyuanpeng All rights reserved.

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.navigationItem.title = "NSUserDefault"
            
            let readItem = UIBarButtonItem(title: "read", style: .done, target: self, action: #selector(ViewController.readInfo))
            let deleteItem = UIBarButtonItem(title: "del", style: .done, target: self, action: #selector(ViewController.deleteInfo))
            self.navigationItem.rightBarButtonItems = [deleteItem, readItem]
            
            self.setUI()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func loadView() {
            super.loadView()
            self.view.backgroundColor = UIColor.white
            if (self.responds(to: #selector(getter: UIViewController.edgesForExtendedLayout)))
            {
                self.edgesForExtendedLayout = UIRectEdge()
            }
        }
        
        // MARK: - 视图
        
        func setUI()
        {
            let textField:UITextField = UITextField(frame: CGRect(x: 10.0, y: 10.0, width: 200.0,height: 30.0))
            self.view.addSubview(textField)
            textField.clearButtonMode = .whileEditing
            textField.returnKeyType = .done
            textField.delegate = self
            textField.textColor = UIColor.red
            textField.layer.borderColor = UIColor.brown.cgColor
            textField.layer.borderWidth = 1.0
        }
        
        // MARK: - 交互
        
        // 保存,或修改
        func saveInfo(_ name:String)
        {
            if (0 <= name.characters.count)
            {
                let userDefault = UserDefaults.standard
                userDefault.set(name, forKey: "name")
                userDefault.synchronize()
                
                let alert = UIAlertView(title: "温馨提示", message: "保存成功", delegate: nil, cancelButtonTitle: "知道了")
                alert.show()
            }
        }
        
        // 读取
        func readInfo() -> String
        {
            let userDefault = UserDefaults.standard
            let name = userDefault.object(forKey: "name") as? String
        
            let alert = UIAlertView(title: "温馨提示", message: "读取成功:\(String(describing: name))", delegate: nil, cancelButtonTitle: "知道了")
            alert.show()
            
            if (name != nil)
            {
                return name!
            }
            
            return ""
        }
        
        // 删除
        func deleteInfo()
        {
            let useDefault = UserDefaults.standard
            useDefault.removeObject(forKey: "name")
         
            let alert = UIAlertView(title: "温馨提示", message: "删除成功", delegate: nil, cancelButtonTitle: "知道了")
            alert.show()
        }
        
        // MARK: - UITextFieldDelegate
        
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            
            textField.resignFirstResponder()
            
            return true
        }
        
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    
            let text = textField.text!
            self.saveInfo(text)
          
            return true
        }
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:swift3.0中UserDefaults的使用

          本文链接:https://www.haomeiwen.com/subject/bxebbxtx.html