如果文本字段为空,请不要保存

时间:2017-05-03 14:44:21

标签: ios swift swift3

我正在做一个大学项目,我遇到了这个问题,我不知道如何修复。当用户点击“注册”按钮时,他的信息将被保存,但如果他没有填写空白,他将收到错误消息。但问题是它会在CoreData中保存一个空文本字段。所以我想" Catch"并且在没有将空文本字段保存到CoreData的情况下给它们一个错误。

import UIKit
import CoreData

class ViewRegister: UIViewController {
    @IBOutlet weak var txtName: UITextField!
    @IBOutlet weak var txtUsername: UITextField!
    @IBOutlet weak var txtPassword: UITextField!
    @IBOutlet weak var txtPhone: UITextField!
    @IBOutlet weak var txtAddress: UITextField!
    @IBOutlet weak var txtCity: UITextField!

    @IBAction func btnRegister(_ sender: Any) {

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let newContact = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
        newContact.setValue(txtName.text, forKey: "name")
        newContact.setValue(txtUsername.text, forKey: "username")
        newContact.setValue(txtPassword.text, forKey: "password")
        newContact.setValue(txtPhone.text , forKey: "phone")
        newContact.setValue(txtAddress.text , forKey: "address")
        newContact.setValue(txtCity.text , forKey: "city")
        do{
            try context.save()
            print ("Saved")
        }
        catch{
            print ("Error")
        }

        if txtCity.text == "" || txtAddress.text == "" || txtPhone.text == "" || txtPassword.text == "" || txtUsername.text == "" || txtName.text == ""
        {
            // Create the alert controller
            let alert = UIAlertController(title: "Ops!!", message: "Please fill in the information", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }

            // Add the actions
            alert.addAction(okAction)
            alert.addAction(cancelAction)

            // Present the controller
            self.present(alert, animated: true, completion: nil)
        }
        else
        {
            txtName.text = ""
            txtUsername.text = ""
            txtPassword.text = ""
            txtPhone.text = ""
            txtAddress.text = ""
            txtCity.text = ""


            // Create the alert controller
            let alert = UIAlertController(title: "Successfully Registered!", message: "Thank you for registering in our application", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }

            // Add the actions
            alert.addAction(okAction)
            alert.addAction(cancelAction)

            // Present the controller
            self.present(alert, animated: true, completion: nil)
        }

    }


}

1 个答案:

答案 0 :(得分:0)

我只能重复@Lepidopteron的评论:为什么你不打电话给你的联系人初始化和保存电话"否则"代码部分?

喜欢这个?

@IBAction func btnRegister(_ sender: Any) {        

    if txtCity.text == "" || txtAddress.text == "" || txtPhone.text == "" || txtPassword.text == "" || txtUsername.text == "" || txtName.text == ""
    {
        // Create the alert controller
        let alert = UIAlertController(title: "Ops!!", message: "Please fill in the information", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }

        // Add the actions
        alert.addAction(okAction)
        alert.addAction(cancelAction)

        // Present the controller
        self.present(alert, animated: true, completion: nil)
    }
    else
    {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let newContact = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
        newContact.setValue(txtName.text, forKey: "name")
        newContact.setValue(txtUsername.text, forKey: "username")
        newContact.setValue(txtPassword.text, forKey: "password")
        newContact.setValue(txtPhone.text , forKey: "phone")
        newContact.setValue(txtAddress.text , forKey: "address")
        newContact.setValue(txtCity.text , forKey: "city")
        do{
            try context.save()
            print ("Saved")
        }
        catch{
             print ("Error")
        }


        txtName.text = ""
        txtUsername.text = ""
        txtPassword.text = ""
        txtPhone.text = ""
        txtAddress.text = ""
        txtCity.text = ""


        // Create the alert controller
        let alert = UIAlertController(title: "Successfully Registered!", message: "Thank you for registering in our application", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }

        // Add the actions
        alert.addAction(okAction)
        alert.addAction(cancelAction)

        // Present the controller
        self.present(alert, animated: true, completion: nil)
    }

}
相关问题