Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. @IBAction func sendSweet(sender: UIBarButtonItem) {
  2.  
  3. var inputTextField: UITextField?
  4. let alert = UIAlertController(title: "New sweet", message: "Enter a sweet", preferredStyle: .alert)
  5. alert.addTextField { (textField: UITextField) in
  6. textField.placeholder = "Your sweet"
  7. inputTextField = textField
  8. }
  9.  
  10. let sendAction = UIAlertAction(title: "Send", style: .default, handler: {
  11. [weak self] (alertAction: UIAlertAction) in
  12. guard let strongSelf = self else { return }
  13.  
  14. if inputTextField?.text != "" {
  15. let newSweet = CKRecord(recordType: "Sweet")
  16. newSweet["content"] = inputTextField?.text as CKRecordValue?
  17.  
  18. let publicData = CKContainer.default().publicCloudDatabase
  19. publicData.save(newSweet, completionHandler: {
  20. (record: CKRecord?, error: Error?) in
  21.  
  22. if error == nil {
  23. // we want ui code to dispatch asychronously in main thread
  24. DispatchQueue.main.async {
  25. strongSelf.tableView.beginUpdates()
  26. strongSelf.sweets.insert(newSweet, at: 0)
  27. let indexPath = IndexPath(row: 0, section: 0)
  28. strongSelf.tableView.insertRows(at: [indexPath], with: .top)
  29. strongSelf.tableView.endUpdates()
  30. }
  31. } else {
  32. if let error = error {
  33. print(error.localizedDescription)
  34. return
  35. }
  36. }
  37. })
  38. }
  39. })
  40.  
  41. alert.addAction(sendAction)
  42. alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  43. present(alert, animated: true, completion: nil)
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement