使用CoreData -Swift3编写的文本文件和标签

时间:2017-07-21 15:10:48

标签: swift core-data swift3

我使用的是CoreData,但我遇到了问题。

所以,我使用滚动视图,在下面的代码中,每次用户按下"添加"时,它会添加标签和文本字段。按钮。我需要将其保存到CoreData(是的,它需要是CoreData)。但是我怎么能这样做呢?我经历了CoreData关系,但无法理解并希望得到帮助。 谢谢你变化很大:))

var count = 0


@IBAction func add(_ sender: Any) {
        count += 1
        let myPoint = count * 100

        if count >= 1 {
            let myAlartController = UIAlertController(title: "Add Item", message: "Please add title and context", preferredStyle: UIAlertControllerStyle.alert)

            myAlartController.addTextField(configurationHandler: ({(firstText: UITextField!) -> Void in firstText.placeholder = "Please add title"}))

            myAlartController.addTextField(configurationHandler: ({(secondText: UITextField!) -> Void in secondText.placeholder = "Please add context"}))


            let okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                (action: UIAlertAction) -> Void in

                //When the bottum is pressed
                let myCGRect = CGRect(x: 0, y: 0, width: 105, height: 30)

                //Label
                let myLabel = UILabel(frame: myCGRect)
                myLabel.backgroundColor = UIColor.orange
                let labelOfAlert = myAlartController.textFields?[0]
                myLabel.text = labelOfAlert?.text

                myLabel.textColor = UIColor.white
                myLabel.layer.position = CGPoint(x: 60, y: (565 + myPoint))


                //textFiled
                let myTextField = UITextField(frame: myCGRect)
                myTextField.backgroundColor = UIColor.gray
                let textOfAlert = myAlartController.textFields?[1]
                myTextField.text = textOfAlert?.text
                myTextField.layer.position = CGPoint(x: 135, y: (565 + myPoint))

                self.labelTopConstrain.constant = (CGFloat(900 + myPoint))
                self.labelBottomConstrain.constant = 20
                self.myScroll.layoutIfNeeded()


                myLabel.tag = self.count
                myTextField.tag = self.count

                self.myScroll.addSubview(myLabel)
                self.myScroll.addSubview(myTextField)


                let testSave = Test(context: myContext)

                testSave.label = myLabel.text
                testSave.textField = myTextField.text

                (UIApplication.shared.delegate as! AppDelegate).saveContext()

            }

            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

            myAlartController.addAction(okButton)
            myAlartController.addAction(cancelAction)

            present(myAlartController, animated: true, completion: nil)
        }/*if*/


    }/**/

0 个答案:

没有答案