CoreData和标签以及文本字段

时间:2017-07-06 08:16:11

标签: ios swift core-data swift3

我正在使用Swift。 我有一个简短的问题,希望你能提供帮助。 我的问题是如何从编程的标签和文本字段中存储数据,并在不同的视图中再次显示它? 任何提示都会对我有帮助。非常感谢你。

2 个答案:

答案 0 :(得分:0)

我建议你创建一个自定义标签和textField(继承自UILabel和UITextField的子类),然后只存储文本,颜色,占位符......

答案 1 :(得分: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)
                        }

}
}
相关问题