首页 > 解决方案 > Swift 5:按下按钮时如何添加文本和日期?

问题描述

我正在努力做到这一点,所以当我按下按钮时,我可以添加单元格的文本和日期。对于日期部分,我尝试查看不同的 Pods 或 Swift Manager Packages 但找不到任何东西。这是到目前为止 addButtonPressed 的代码。我想添加代码,以便我也可以添加日期:

@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        var textField = UITextField()
        
        let alert = UIAlertController(title: "Create New Category", message: "", preferredStyle: .alert)
        
        let action = UIAlertAction(title: "Add", style: .default) { (action) in
            let newProduct = Product(context: self.context)
            newProduct.name = textField.text
            
            self.products.append(newProduct)
            self.saveProducts()
        }
        
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Create new category"
            textField = alertTextField
        }
        
        alert.addAction(action)
        
        present(alert, animated: true, completion: nil)
    }

这是迄今为止我所拥有的模拟器。

谢谢!

标签: swiftdatepicker

解决方案


推荐阅读