Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @IBAction func sendSweet(sender: UIBarButtonItem) {
- var inputTextField: UITextField?
- let alert = UIAlertController(title: "New sweet", message: "Enter a sweet", preferredStyle: .alert)
- alert.addTextField { (textField: UITextField) in
- textField.placeholder = "Your sweet"
- inputTextField = textField
- }
- let sendAction = UIAlertAction(title: "Send", style: .default, handler: {
- [weak self] (alertAction: UIAlertAction) in
- guard let strongSelf = self else { return }
- if inputTextField?.text != "" {
- let newSweet = CKRecord(recordType: "Sweet")
- newSweet["content"] = inputTextField?.text as CKRecordValue?
- let publicData = CKContainer.default().publicCloudDatabase
- publicData.save(newSweet, completionHandler: {
- (record: CKRecord?, error: Error?) in
- if error == nil {
- // we want ui code to dispatch asychronously in main thread
- DispatchQueue.main.async {
- strongSelf.tableView.beginUpdates()
- strongSelf.sweets.insert(newSweet, at: 0)
- let indexPath = IndexPath(row: 0, section: 0)
- strongSelf.tableView.insertRows(at: [indexPath], with: .top)
- strongSelf.tableView.endUpdates()
- }
- } else {
- if let error = error {
- print(error.localizedDescription)
- return
- }
- }
- })
- }
- })
- alert.addAction(sendAction)
- alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
- present(alert, animated: true, completion: nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement