forked from NikantVohra/HackerNewsClient-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostViewController.swift
More file actions
78 lines (59 loc) · 2.35 KB
/
PostViewController.swift
File metadata and controls
78 lines (59 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// PostViewController.swift
// HackerNews
//
// Created by Vohra, Nikant on 07/04/15.
// Copyright (c) 2015 Vohra, Nikant. All rights reserved.
//
import UIKit
class PostViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var urlTextField: DesignableTextField!
@IBOutlet weak var titleTextField: DesignableTextField!
@IBOutlet weak var textView: UITextView!{
didSet {
self.textView.layer.borderColor = UIColor.lightGrayColor().CGColor
self.textView.layer.borderWidth = 1.0
self.textView.layer.cornerRadius = 5
}
}
@IBOutlet weak var done: UIBarButtonItem!
@IBAction func done(sender: UIBarButtonItem) {
let title = titleTextField.text
let url = urlTextField.text
let text = textView.text
if title != nil && title != "" {
view.showLoading()
HNManager.sharedManager().submitPostWithTitle(title, link: url, text: text, completion: { (success) -> Void in
self.view.hideLoading()
if(success) {
self.dismissViewControllerAnimated(true, completion: nil)
}
else {
self.showAlert("Something went wrong. Your post wasn't sent. Try again and save your text just in case.")
}
})
}
else {
self.showAlert("Please enter a valid title")
}
}
func showAlert(message : String) {
self.view.hideLoading()
var alert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
@IBOutlet weak var cancel: UIBarButtonItem!
@IBAction func cancel(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.titleTextField.becomeFirstResponder()
}
func textFieldDidEndEditing(textField: UITextField) {
if(textField == self.titleTextField) {
self.urlTextField.becomeFirstResponder()
}
}
}