2017-07-06 9 views
-1

私はSwiftを使用しています。 私は簡単な質問があり、あなたが手伝ってくれることを願っています。 私の質問は、プログラムで作成したラベルとテキストフィールドのデータを保存し、別のビューで再び表示するにはどうすればいいですか? ご意見は私に役立ちます。どうもありがとうございました。CoreDataとラベルとテキストフィールド

答えて

0

私はあなたがカスタムラベルとTextField(UILabelやUITextFieldの継承サブクラス)を作成することをお勧めしてから、テキストのみ、色、プレースホルダを保存し、...

0
import UIKit 
import CoreData 

class ViewController: UIViewController , UITextFieldDelegate 
{ 

    var txtName:UITextField! 
    var txtEmail:UITextField! 
    var txtPassword:UITextField! 
    var btnfirst :UIButton! 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    var managedContext:NSManagedObjectContext! 
    var entity:NSEntityDescription! 
    var stname:String! 
    var stpass:String! 
    var stenno:String! 


    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     txtName = UITextField() 
     txtName.frame = CGRect(x: 10, y: 30, width: 300, height: 30) 
     txtName.borderStyle = UITextBorderStyle.roundedRect 
     txtName.backgroundColor = .yellow 
     txtName.placeholder = "Enter Your Name" 
     txtName.autocorrectionType = .no 
     txtName.clearButtonMode = .always 
     self.view.addSubview(txtName) 

     txtEmail = UITextField() 
     txtEmail.frame = CGRect(x: 10, y: 70, width: 300, height: 30) 
     txtEmail.backgroundColor = .yellow 
     txtEmail.placeholder = "Enter Your Email Address" 
     txtEmail.keyboardType = .emailAddress 
     txtEmail.autocorrectionType = .no 
     txtEmail.clearButtonMode = .always 
     txtEmail.borderStyle = UITextBorderStyle.roundedRect 
     view.addSubview(txtEmail) 

     txtPassword = UITextField() 
     txtPassword.placeholder = "Enter Password Your Choice" 
     txtPassword.frame = CGRect(x: 10, y: 110, width: 300, height: 30) 
     txtPassword.borderStyle = UITextBorderStyle.roundedRect 
     txtPassword.backgroundColor = .yellow 
     txtPassword.clearButtonMode = .always 
     txtPassword.autocorrectionType = .no 
     txtPassword.isSecureTextEntry = true 
     view.addSubview(txtPassword) 

    btnfirst = UIButton(type: .system) 
     btnfirst.setTitle("Press", for: .normal) 
     btnfirst.setTitleColor(.red, for: .normal) 
     btnfirst.frame = CGRect(x: 100, y: 200, width: 100, height: 30) 
     btnfirst.addTarget(self, action: #selector(benpress(sender:)),for: .touchUpInside) 
     btnfirst.backgroundColor = .green 
    self.view.addSubview(btnfirst) 
} 
func benpress(sender :UIButton) 
{ 
     stname = txtName.text 
     stenno = txtEmail.text 
     stemail = txtPassword.text 
self.managedContext = self.appDelegate.persistentContainer.viewContext 
    entity = NSEntityDescription.entity(forEntityName: "Student", in: managedContext) 
         let storeStudent = NSManagedObject.init(entity: entity, insertInto: managedContext) 
         storeStudent.setValue(stname, forKey: "name") 
         storeStudent.setValue(stpass, forKey: "password") 
         storeStudent.setValue(stemail, forKey: "email") 

         do { 
          try managedContext.save() 

         } catch let error as Error! { 
          print(error.localizedDescription) 
         } 

} 
} 
+0

はどうもありがとうございました。私はそれを試み、それは私に "storeStudent = NSManagedObject.init(entity:entity、insertInto:managedContext)の行にエラーが表示されます"というエラーは "致命的なエラーです:オプション値をアンラッピングしている間に予期せずnilが見つかりました。理由は何ですか? – dropscar

+0

申し訳ありませんが、これはうまくいきません:(そして、私はそれをちょっと変えてしまいましたが、実行するようですが、アプリケーションを再オープンするたびにデータがなくなってしまいます。あなたは私に与えることができますか?あなたの助けを大変ありがとう – dropscar

関連する問題