2017-05-03 2 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

"else"코드 부분에 연락처 초기화 및 저장 호출을 호출하지 않는 이유는 무엇입니까? 이전에 if-condition이 점검 되었기 때문에 값이 유효하지 않은가? – Lepidopteron

+2

당신은 'context.save()'를 무조건 말하는 사람입니다. 그렇게하고 싶지 않다면 그렇게하지 마십시오. – matt

+0

@matt 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

덕분에 효과가있었습니다. 생각하지 않았습니다. –

관련 문제