2017-07-06 52 views
-1

我正在使用Swift。 我有一个快速的问题,并希望你能帮助。 我的问题是我如何从一个程序化的标签和文本字段存储数据,并在不同的视图中再次显示它? 任何提示将帮助我。非常感谢你。CoreData和标签和文本框

回答

0

我建议你创建一个自定义标签和文本框(子类,从的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

非常感谢你。我试过了,它给了我一个“let storeStudent = NSManagedObject.init(实体:entity,insertInto:managedContext)”错误,“错误是”致命错误:意外地发现零,同时展开一个可选值。你有什么想法,为什么? – dropscar

+0

我很抱歉,但这不起作用:(或者我改变了它有点我的自我,似乎运行,但每次我重新打开应用程序的数据已经消失,所以这个代码没有正确存储。你可以给我吗?非常感谢你的帮助 – dropscar