forked from chukon/Week-5-HideKeyBoard-ScrollView---Start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode for steps.txt
160 lines (127 loc) · 5.94 KB
/
code for steps.txt
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 1) Add MessageBox function using AlertView
func MsgBox(message:String)
{
//Create Alert
let alert = UIAlertView()
alert.title = "Alert"
alert.message = message
alert.addButtonWithTitle("OK")
alert.show()
}
// 2) Add touchesBegan function to catch screen tap and resign keyboard
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//forces resign first responder and hides keyboard
txtFirst.endEditing(true)
txtLast.endEditing(true)
txtEmail.endEditing(true)
}
// 3) Add DismissKeyboard function to resign keyboard on all textboxes
func DismissKeyboard(){
//forces resign first responder and hides keyboard
txtFirst.endEditing(true)
txtLast.endEditing(true)
txtEmail.endEditing(true)
}
// 4) Add textFieldShouldReturn function. This is called when 'return' key pressed on any UITextField. return NO to ignore.
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true;
}
// 5) Add textFieldDidBeginEditing function. ScrollPoint when entering UItextfied
func textFieldDidBeginEditing(textField:UITextField){
var scrollPoint:CGPoint
scrollPoint = CGPointMake(0, textField.frame.origin.y)
ScrollView.setContentOffset(scrollPoint, animated: true)
}
// 6) Add textFieldDidEndEditing function. ScrollPoint when done editing UItextfied
func textFieldDidEndEditing(textField:UITextField){
ScrollView.setContentOffset(CGPointZero, animated: true)
}
// 7) Add textViewDidBeginEditing function. ScrollPoint when entering UItextView
func textViewDidBeginEditing(textField:UITextView){
var scrollPoint:CGPoint
scrollPoint = CGPointMake(0, textField.frame.origin.y)
ScrollView.setContentOffset(scrollPoint, animated: true)
}
// 8) Add textViewDidEndEditing function. ScrollPoint when done ending UItextView
func textViewDidEndEditing(textField:UITextView){
ScrollView.setContentOffset(CGPointZero, animated: true)
}
// 9) Modify existing viewDidLoad function. Default Function to load when view is shown
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Add Below Code. Read Comments
//Looks for single or multiple taps
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
//Adds tap gesture to ScrollView which will call DismissKeyboard and hide keyboard
ScrollView.addGestureRecognizer(tap)
//gets ScreenSize of current device
let size: CGRect = UIScreen.mainScreen().bounds
//Go to left = 0 pixels, top = 50 pixels, width or device in pixels, height of device in pixels
ScrollView.frame = CGRectMake(0, 50, size.width, size.height)
//Set focus on txtFirst
txtFirst.becomeFirstResponder()
}
// 10) Default didReceiveMemoryWarning Function to dispose of memory automatically.Leave this function alone
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 11) Modify btnBack function. Goes back to original ScrollView position
@IBAction func btnBack(sender: UIButton) {
//Add Below Code. Read Comments
//Set ScrollPoint and Go to ZERO location (Top) of ScrollView - animated
ScrollView.setContentOffset(CGPointZero, animated: true)
//set txtFirst as firstresponder
txtFirst.becomeFirstResponder()
}
// 12) Modify btnView functions. Goes to position in ScrollView where the btnBack button is
@IBAction func btnView(sender: UIButton) {
//Add Below Code. Read Comments
//hide keyboard
DismissKeyboard()
//declare CGPoint scrollPoint
var scrollPoint:CGPoint
//X = from Left, Y = from Top
//get X = 0, Y = location of btnBack Y
scrollPoint = CGPointMake(0, btnBack.frame.origin.y)
//Set ScrollPoint and Go to animated
ScrollView.setContentOffset(scrollPoint, animated: true)
}
// 13) Modify btnSave function. Validates UITextfields have content, formats text to place in UITextView txtContacts, Goes to location of btnBack button.
@IBAction func btnSave(sender: UIButton) {
//Add Below Code. Read Comments
//hide keyboard
DismissKeyboard()
//validation that all fields are entered
if (txtFirst.text=="" || txtLast.text=="" || txtEmail.text=="")
{
//Call MessageBox if any fields are empty
MsgBox("All fields required, please correct")
}
else
{
//Check if txtContacts (UITextView) is empty
if (txtContacts.text=="")
{
//if empty then add text and newline
txtContacts.text = "MyContacts \n"
}
//format text
//existing contents of txtContacts, newline, txtFirst, newline, txtLast, newline, txtEmail, newline
txtContacts.text = "\(txtContacts.text!) \n\(txtFirst.text!) \n\(txtLast.text!) \n\(txtEmail.text!)\n"
//clear textboxes
txtFirst.text = ""
txtLast.text = ""
txtEmail.text = ""
//load scrollview
//declare CGPoint scrollPoint
var scrollPoint:CGPoint
//X = from Left, Y = from Top
//get X = 0, Y = location of btnBack Y
scrollPoint = CGPointMake(0, btnBack.frame.origin.y)
//Set ScrollPoint and Go to animated
ScrollView.setContentOffset(scrollPoint, animated: true)
}
}