-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddPasswordVC.m
executable file
·170 lines (142 loc) · 6.28 KB
/
AddPasswordVC.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
//
// AddPasswordVC.m
// Calculator
//
// Created by Corey Allen Pett on 9/23/15.
// Copyright (c) 2015 Corey Allen Pett. All rights reserved.
//
#import "AddPasswordVC.h"
#import "ViewPasswordVC.h"
@interface AddPasswordVC () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *titleTextfield;
@property (weak, nonatomic) IBOutlet UITextField *usernameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextfield;
@property (weak, nonatomic) IBOutlet UITextField *websiteTextfield;
@property (weak, nonatomic) IBOutlet UITextField *notesTextfield;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation AddPasswordVC
//If no password exists already, set up passwordStorage object
-(PasswordStorage *)passwordStorage
{
if(!_passwordStorage) _passwordStorage = [[PasswordStorage alloc] init];
return _passwordStorage;
}
- (void)viewDidLoad {
[super viewDidLoad];
//Manipulate textfield to be rounded
self.titleTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.usernameTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.passwordTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.websiteTextfield.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.titleTextfield.delegate = self;
self.usernameTextfield.delegate = self;
self.passwordTextfield.delegate = self;
self.websiteTextfield.delegate = self;
self.notesTextfield.delegate = self;
//If editing contact, place current contact information into the contact form
if (self.passwordStorage.selectedPassword) {
self.titleTextfield.text = self.passwordStorage.selectedPassword.title;
self.usernameTextfield.text = self.passwordStorage.selectedPassword.username;
self.passwordTextfield.text = self.passwordStorage.selectedPassword.password;
self.notesTextfield.text = self.passwordStorage.selectedPassword.notes;
self.websiteTextfield.text = self.passwordStorage.selectedPassword.website;
}
//Enable save button if the first name textfield is filled out
if (self.titleTextfield.text.length > 0){
self.saveButton.enabled = YES;
}
else{
self.saveButton.enabled = NO;
}
//When editing textfields, enable clear button mode
self.titleTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.titleTextfield.clearsOnBeginEditing = YES;
self.usernameTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.usernameTextfield.clearsOnBeginEditing = YES;
self.passwordTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.passwordTextfield.clearsOnBeginEditing = YES;
self.websiteTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.websiteTextfield.clearsOnBeginEditing = YES;
self.notesTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
self.notesTextfield.clearsOnBeginEditing = YES;
}
//Dismiss keyboard method for all textfields
-(void)dismissKeyboard
{
[self.titleTextfield resignFirstResponder];
[self.usernameTextfield resignFirstResponder];
[self.passwordTextfield resignFirstResponder];
[self.websiteTextfield resignFirstResponder];
[self.notesTextfield resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveButton:(id)sender
{
//Save user contact information if user is editing a current contact
if(self.passwordStorage.selectedPassword){
[self.passwordStorage savePassword:self.titleTextfield.text
username:self.usernameTextfield.text
password:self.passwordTextfield.text
website:self.websiteTextfield.text
notes:self.notesTextfield.text];
}
//If user is not editing a current contact, create new contact
else {
[self.passwordStorage createPassword:self.titleTextfield.text
username:self.usernameTextfield.text
password:self.passwordTextfield.text
website:self.websiteTextfield.text
notes:self.notesTextfield.text];
}
//Pass the data back to view the contact
ViewPasswordVC *destinationController = [[ViewPasswordVC alloc] init];
destinationController.passwordStorage = self.passwordStorage;
[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