-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddContactVC.m
executable file
·217 lines (181 loc) · 8.86 KB
/
AddContactVC.m
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// AddContactVC.m
// Calculator
//
// Created by Corey Allen Pett on 9/19/15.
// Copyright (c) 2015 Corey Allen Pett. All rights reserved.
//
#import "AddContactVC.h"
#import "ViewContactVC.h"
@interface AddContactVC () <UITextFieldDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *firstNameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *lastNameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *companyTextfield;
@property (weak, nonatomic) IBOutlet UITextField *mobileNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *homeNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *workNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *emailTextfield;
@property (weak, nonatomic) IBOutlet UITextField *webpageTextfield;
@property (weak, nonatomic) IBOutlet UITextField *notesTextfield;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation AddContactVC
//If no contact exists already, set up contactStorage object
-(ContactStorage *)contactStorage
{
if(!_contactStorage) _contactStorage = [[ContactStorage alloc] init];
return _contactStorage;
}
- (void)viewDidLoad {
[super viewDidLoad];
//Used to fix scrollview from not scrolling
self.scrollView.delegate = self;
UIView *contentView;
[self.scrollView addSubview:contentView];
[self.scrollView setContentSize:contentView.frame.size];
//Manipulate textfield to be rounded
self.firstNameTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.lastNameTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.companyTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.mobileNumberTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.homeNumberTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.workNumberTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.emailTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.webpageTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.notesTextfield.borderStyle = UITextBorderStyleRoundedRect;
//Dismiss keyboard when user presses outside of textfield
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
self.firstNameTextfield.delegate = self;
self.lastNameTextfield.delegate = self;
self.companyTextfield.delegate = self;
self.mobileNumberTextfield.delegate = self;
self.homeNumberTextfield.delegate = self;
self.workNumberTextfield.delegate = self;
self.emailTextfield.delegate = self;
self.notesTextfield.delegate = self;
self.webpageTextfield.delegate = self;
//If editing contact, place current contact information into the contact form
if(self.contactStorage.selectedContact) {
self.firstNameTextfield.text = self.contactStorage.selectedContact.firstName;
self.lastNameTextfield.text = self.contactStorage.selectedContact.lastName;
self.companyTextfield.text = self.contactStorage.selectedContact.company;
self.mobileNumberTextfield.text = self.contactStorage.selectedContact.mobileNumber;
self.homeNumberTextfield.text = self.contactStorage.selectedContact.homeNumber;
self.workNumberTextfield.text = self.contactStorage.selectedContact.workNumber;
self.emailTextfield.text = self.contactStorage.selectedContact.email;
self.notesTextfield.text = self.contactStorage.selectedContact.notes;
self.webpageTextfield.text = self.contactStorage.selectedContact.website;
}
//Enable save button if the first name textfield is filled out
if (self.firstNameTextfield.text.length > 0){
self.saveButton.enabled = YES;
}
else{
self.saveButton.enabled = NO;
}
//When editing textfields, enable clear button mode
self.firstNameTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.firstNameTextfield.clearButtonMode = YES;
self.lastNameTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.lastNameTextfield.clearButtonMode = YES;
self.companyTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.companyTextfield.clearButtonMode = YES;
self.mobileNumberTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.mobileNumberTextfield.clearButtonMode = YES;
self.homeNumberTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.homeNumberTextfield.clearButtonMode = YES;
self.workNumberTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.workNumberTextfield.clearButtonMode = YES;
self.emailTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.emailTextfield.clearButtonMode = YES;
self.notesTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.notesTextfield.clearButtonMode = YES;
self.webpageTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.webpageTextfield.clearButtonMode = YES;
}
//Dismiss keyboard method for all textfields
-(void)dismissKeyboard {
[self.firstNameTextfield resignFirstResponder];
[self.lastNameTextfield resignFirstResponder];
[self.companyTextfield resignFirstResponder];
[self.mobileNumberTextfield resignFirstResponder];
[self.homeNumberTextfield resignFirstResponder];
[self.workNumberTextfield resignFirstResponder];
[self.emailTextfield resignFirstResponder];
[self.notesTextfield resignFirstResponder];
[self.webpageTextfield resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveButton:(id)sender
{
//Save user contact information when user is editing a current contact
if(self.contactStorage.selectedContact){
[self.contactStorage saveContact:self.firstNameTextfield.text
lastName:self.lastNameTextfield.text
company:self.companyTextfield.text
mobileNumber:self.mobileNumberTextfield.text
homeNumber:self.homeNumberTextfield.text
workNumber:self.workNumberTextfield.text
email:self.emailTextfield.text
notes:self.notesTextfield.text
website:self.webpageTextfield.text];
}
//If user is not editing a current contact, create new contact
else {
[self.contactStorage createContact:self.firstNameTextfield.text
lastName:self.lastNameTextfield.text
company:self.companyTextfield.text
mobileNumber:self.mobileNumberTextfield.text
homeNumber:self.homeNumberTextfield.text
workNumber:self.workNumberTextfield.text
email:self.emailTextfield.text
notes:self.notesTextfield.text
website:self.webpageTextfield.text];
}
//Pass the data back to view the contact
ViewContactVC *destinationController = [[ViewContactVC alloc] init];
destinationController.contactStorage = self.contactStorage;
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancelButton:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
//Enable save button when user fills out the firstName.textfield.text
- (IBAction)editingChanged:(UITextField *)textField
{
//if text field is empty, disable the button
self.saveButton.enabled = textField.text.length > 0;
}
//Called when textField start editing a textfield
//Moves the textfield the user is editing up to the top of the scrollview for visiblity
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.scrollView setContentOffset:CGPointMake(0,textField.center.y-100) animated:YES];
}
//Called when user clicks on the return button.
//Move user to the next textfield input
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder *nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[self.scrollView setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES];
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
[self.scrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}
return NO;
}
@end