自定义委托nil且不保存userDefaults

时间:2019-01-02 04:46:30

标签: ios swift delegates uitextfield

我在UIView中创建一个委托,以将数据传递到控制器,然后将其保存在userdefaults中。但是当我尝试保存它时,它返回nil。如何保存它而不返回nil。

这是我在UIView和协议中的代码

protocol SaveUpdateInputDelegate {
   func saveInputText()
   func fetchInputText()
}

class FormUIView: UIView {

var delegate: SaveUpdateInputDelegate?

let nameLabel = ProfileCustomLabel(textName: "Name")
let nameTextField = CustomTextField(placeholderName: "Name")

let emailLabel = ProfileCustomLabel(textName: "Email")
let emailTextField = CustomTextField(placeholderName: "Email")

let titleLabel = ProfileCustomLabel(textName: "Title")
let titleTextField = CustomTextField(placeholderName: "Title")

let locationLabel = ProfileCustomLabel(textName: "Location")
let locationTextField = CustomTextField(placeholderName: "Location")

这是我的控制器代码,具有用于保存输入文本并将其保存在userdefaults中的按钮

class ProfileController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SaveUpdateInputDelegate {

// I need to initialized this to create autolayout
let formView: FormUIView = FormUIView()

let saveButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Save", for: .normal)
    btn.setTitleColor(.white, for: .normal)
    btn.backgroundColor = #colorLiteral(red: 0.1254901961, green: 0.7411764706, blue: 1, alpha: 1)
    btn.layer.cornerRadius = 18
    btn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
    return btn
}()

override func viewDidLoad() {
    super.viewDidLoad()
    formView.delegate = self
    handleSave()
}

@objc func handleSave() {
    print("saved...")
    guard let data = UserDefaults.standard.object(forKey: "profileImageEditedKey") as? Data else { return }
    let image = UIImage.init(data: data)
    DispatchQueue.main.async {
        self.profileImageView.image = image?.withRenderingMode(.alwaysOriginal)
    }

    saveInputText()
    fetchInputText()
}

func saveInputText() {
    UserDefaults.standard.set(formView.nameTextField.text!, forKey: "nameTextFieldKey")
    UserDefaults.standard.set(formView.emailTextField.text!, forKey: "emailTextFieldKey")
    UserDefaults.standard.set(formView.titleTextField.text!, forKey: "titleTextFieldKey")
    UserDefaults.standard.set(formView.locationTextField.text!, forKey: "locationTextFieldKey")
    UserDefaults.standard.synchronize()
}

func fetchInputText() {
    let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
    let email = UserDefaults.standard.value(forKey: "emailTextFieldKey") as? String ?? ""
    let title = UserDefaults.standard.value(forKey: "titleTextFieldKey") as? String ?? ""
    let location = UserDefaults.standard.value(forKey: "locationTextFieldKey") as? String ?? ""

    formView.nameTextField.text = name
    formView.emailTextField.text = email
    formView.titleTextField.text = title
    formView.locationTextField.text = location
}

3 个答案:

答案 0 :(得分:1)

我不明白您为什么要使用委托来传递不必要的数据或做错了事。

所有都是文本字段,因此您可以直接将textField Delegate设置为ViewController ,因此无需在周围传递值,这不是在此处正确使用委托的目的

假设您无法执行此操作,则每次textFieldShouldChangeCharactertextFieldDidEndEditing时都需要调用委托  打电话,但是正如我所说的那样,这是错误的方式

第一步:确认所有正确的信息都保存在其中

第2步:UserDefault已经具有获取字符串的方法

替换此

let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""

 let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""

和其他人。

答案 1 :(得分:1)

您仅应在fetchInputText中调用viewDidLoad,仅用于设置textFields中的UserDefaults文本。当前您正在呼叫handleSave,当saveInputText没有值(或默认值)时,该内部调用textFields。如果您没有为text中的任何Storyboard/Xib设置任何默认textFields,则可能会崩溃。

override func viewDidLoad() {
    super.viewDidLoad()
    formView.delegate = self

    fetchInputText()
}

使用delegate的目的似乎没有意义,因为FormUIView并没有传播任何delegate要处理的内容。这可能是错误的,因为我不知道FormUIView的完整实现。

答案 2 :(得分:0)

应该从formView.saveInputText()函数而不是viewDidAppear()函数中调用viewDidLoad()