2017-05-03 58 views
-2

我工作的一個大學項目,我是有這個問題,我不知道如何解決。當用戶點擊註冊按鈕他的信息將被保存,但如果他沒有填補空白,他將得到一個錯誤信息。但問題是它會在CoreData中保存一個空文本字段。所以我想「抓住」,並給他們一個錯誤,而不是將一個空的文本字段保存到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) 
     } 

    } 


} 
+0

爲什麼不在你的「其他」代碼部分中調用你的聯繫人初始化和保存調用。由於之前檢查過if條件,所以這些值是否無效? – Lepidopteron

+2

你是誰在說'context.save()'無條件地之一。如果你不想這樣做,那麼不要這樣做。 – matt

+0

@馬特我在Xcode是新的,請幫助 –

回答

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) 
    } 

} 
+0

感謝它的工作,我沒有想過它 –

相關問題