This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
forked from peacecorps/peacetrack-ios
-
Notifications
You must be signed in to change notification settings - Fork 5
/
VCMain.swift
executable file
·305 lines (271 loc) · 13.6 KB
/
VCMain.swift
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// VCMain.swift
// swiftPeaceTrack<3
//
// Created by Shelagh McGowan on 7/4/14.
// Copyright (c) 2014 Shelagh McGowan. All rights reserved.
// Citing https://github.com/GabrielMassana/Picker-iOS8 :Picker menu views code
import Foundation
import UIKit
import CoreData
class VCMain: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, NSURLConnectionDelegate, UITextFieldDelegate{
//configure text boxes
@IBOutlet var txtFullName: UITextField!
@IBOutlet var txtVolunteerID: UITextField!
//instantiate 2 picker views
var countryPicker = UIPickerView()
var sectorPicker = UIPickerView()
//instantiate 2 NSArrays
var countryPickerData = NSArray()
var sectorPickerData = NSArray()
//instantiate 2 NSMutabaleArrays
var countriesArray: NSMutableArray = NSMutableArray()
var sectorsArray: NSMutableArray = NSMutableArray()
//create 2 labels to show picker view selection
@IBOutlet var countryLabel : UILabel! = nil
@IBOutlet var sectorLabel : UILabel! = nil
//declare the initial view and window
var actionView: UIView = UIView()
var window: UIWindow? = nil
//this is related to the fields in the 'create account' view
//but it's also resetting the value every time the app is launched, I think, which doesn't really matter cuz this view is only seen once anyway.
var empty: Bool = true
//how do I declare this without it being true or false right off the bat? This boolean is false if the VCMain view hasn't been viewed before, and true if it has. Used in the view did load method to determine whether the segue occurs or not.
var viewed: Bool = false
//what happens when the root view loads
override func viewDidLoad() {
super.viewDidLoad()
txtFullName.delegate = self
txtVolunteerID.delegate = self
// This is a rogue way not to show the create account screen to the user more than once. Will need to replace with a better way, or simply with a less animated segue(using NSUserDefaults? Swift?)
if viewed == true{
self.performSegueWithIdentifier("mySegue", sender: self)
}
var delegate = UIApplication.sharedApplication()
//We have an array of views here
var myWindow: UIWindow? = delegate.keyWindow
var myWindow2: NSArray = delegate.windows
if let myWindow: UIWindow = UIApplication.sharedApplication().keyWindow
{
window = myWindow
}
else {
window = myWindow2[0] as? UIWindow
}
countryPicker.backgroundColor = UIColor.whiteColor()
sectorPicker.backgroundColor = UIColor.whiteColor()
actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height.0, UIScreen.mainScreen().bounds.size.width, 260.0)
//two variables to hold the plists
var filePath = NSBundle.mainBundle().pathForResource("Property List", ofType: "plist")
var filePath2 = NSBundle.mainBundle().pathForResource("sectors", ofType: "plist")
//Add the contents of filePath (PropertyList.plist) to the NSMutableArray "countriesArray".
countriesArray = NSMutableArray(contentsOfFile: filePath!) //added ! here to fix beta 6 error
//Add the contents of filePath2 (sectors.plist) to the NSMutableArray "sectorsArray"
sectorsArray = NSMutableArray(contentsOfFile: filePath2!) //added ! here to fix beta 6 error
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
txtFullName.resignFirstResponder()
txtVolunteerID.resignFirstResponder()
return true
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
// txtFullName.resignFirstResponder()
// txtVolunteerID.resignFirstResponder()
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//conditionally creates a new user object and saves it to the database. also prints out the object in the console.
@IBAction func addAccount(){
//create a new variable appDel to store app delegate
//cast delegate of type UIApplication to our delegate type AppDelegate
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
//get our ManagedObjectContext from our app delegate. now we are able to save and load information from our SQLite file.
var context:NSManagedObjectContext = appDel.managedObjectContext
//make a new user object that will be inserting itself into the database
//cast it back to an NSManagedObject
var newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject
newUser.setValue(""+txtFullName.text, forKey: "username")
newUser.setValue(""+txtVolunteerID.text, forKey: "password")
newUser.setValue(""+countryLabel.text, forKey: "post")
newUser.setValue(""+sectorLabel.text, forKey: "sector")
//if all the fields are full...
if txtFullName.text != "" && txtVolunteerID.text != "" && countryLabel.text != "No country" && sectorLabel.text != "No sector" {
//empty is no longer true
empty==false
//save the object
context.save(nil)
//print object to the console
println(newUser)
println("Object Saved")
//perfom the custom segue
self.performSegueWithIdentifier("mySegue", sender: self)
//perhaps this is a better place to set viewed to true? only if user has created an account will they not see the create account view. Sounds like a good plan lol
//viewed = true
}
else{
//show the alert view
var myAlertView = UIAlertView()
myAlertView.title = "Can't Create Account"
myAlertView.message = "Please Fill All Fields"
myAlertView.addButtonWithTitle("OK")
myAlertView.show()
}
}
/***overrides the prepareForSegue method so that I can use it for mySegue to call my segue (from create account screen to home screen) programmatically instead of using storyboard.***/
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "mySegue"{
}
}
@IBAction func openCountryPicker(sender : UIButton)
{
let kSCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
countryPicker.frame = CGRectMake(0.0, 44.0,kSCREEN_WIDTH, 216.0)
countryPicker.dataSource = self
countryPicker.delegate = self
countryPicker.showsSelectionIndicator = true;
countryPicker.backgroundColor = UIColor.whiteColor()
var pickerDateToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
pickerDateToolbar.barStyle = UIBarStyle.Black
pickerDateToolbar.barTintColor = UIColor.grayColor()
pickerDateToolbar.translucent = true
var barItems = NSMutableArray()
var labelCancel = UILabel()
labelCancel.text = "Cancel"
var titleCancel = UIBarButtonItem(title: labelCancel.text, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("cancelPickerSelectionButtonClicked:"))
titleCancel.tintColor = UIColor.whiteColor()
barItems.addObject(titleCancel)
var flexSpace: UIBarButtonItem
flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
barItems.addObject(flexSpace)
countryPickerData = countriesArray
countryPicker.selectRow(1, inComponent: 0, animated: false)
let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("countryDoneClicked:"))
doneBtn.tintColor = UIColor.whiteColor()
barItems.addObject(doneBtn)
pickerDateToolbar.setItems(barItems, animated: true)
actionView.addSubview(pickerDateToolbar)
actionView.addSubview(countryPicker)
if (window != nil) {
window!.addSubview(actionView)
}
else
{
self.view.addSubview(actionView)
}
UIView.animateWithDuration(0.2, animations: {
self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - 260.0, UIScreen.mainScreen().bounds.size.width, 260.0)
})
}
@IBAction func openSectorPicker(sender : UIButton)
{
let kSCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
sectorPicker.frame = CGRectMake(0.0, 44.0,kSCREEN_WIDTH, 216.0)
sectorPicker.dataSource = self
sectorPicker.delegate = self
sectorPicker.showsSelectionIndicator = true;
sectorPicker.backgroundColor = UIColor.whiteColor()
var pickerDateToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
pickerDateToolbar.barStyle = UIBarStyle.Black
pickerDateToolbar.barTintColor = UIColor.grayColor()
pickerDateToolbar.translucent = true
var barItems = NSMutableArray()
var labelCancel = UILabel()
labelCancel.text = "Cancel"
var titleCancel = UIBarButtonItem(title: labelCancel.text, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("cancelPickerSelectionButtonClicked:"))
titleCancel.tintColor = UIColor.whiteColor()
barItems.addObject(titleCancel)
var flexSpace: UIBarButtonItem
flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
barItems.addObject(flexSpace)
sectorPickerData = sectorsArray
sectorPicker.selectRow(1, inComponent: 0, animated: false)
let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("sectorDoneClicked:"))
doneBtn.tintColor = UIColor.whiteColor()
barItems.addObject(doneBtn)
pickerDateToolbar.setItems(barItems, animated: true)
actionView.addSubview(pickerDateToolbar)
actionView.addSubview(sectorPicker)
if (window != nil) {
window!.addSubview(actionView)
}
else
{
self.view.addSubview(actionView)
}
UIView.animateWithDuration(0.2, animations: {
self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - 260.0, UIScreen.mainScreen().bounds.size.width, 260.0)
})
}
//also working for country picker cancel button
func cancelPickerSelectionButtonClicked(sender: UIBarButtonItem) {
UIView.animateWithDuration(0.2, animations: {
self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width, 260.0)
}, completion: { _ in
for obj: AnyObject in self.actionView.subviews {
if let view = obj as? UIView
{
view.removeFromSuperview()
}
}
})
}
func countryDoneClicked(sender: UIBarButtonItem) {
var myRow = countryPicker.selectedRowInComponent(0)
countryLabel.text = countryPickerData.objectAtIndex(myRow) as NSString
UIView.animateWithDuration(0.2, animations: {
self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width, 260.0)
}, completion: { _ in
for obj: AnyObject in self.actionView.subviews {
if let view = obj as? UIView
{
view.removeFromSuperview()
}
}
})
}
func sectorDoneClicked(sender: UIBarButtonItem) {
var myRow = sectorPicker.selectedRowInComponent(0)
sectorLabel.text = sectorPickerData.objectAtIndex(myRow) as NSString
UIView.animateWithDuration(0.2, animations: {
self.actionView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width, 260.0)
}, completion: { _ in
for obj: AnyObject in self.actionView.subviews {
if let view = obj as? UIView
{
view.removeFromSuperview()
}
}
})
}
// MARK - Picker delegate
//sets up the number of rows in the component
func pickerView(_pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
var returnNumb: Int = 0
if (_pickerView == countryPicker)
{
returnNumb=countryPickerData.count
}
else if (_pickerView == sectorPicker){
returnNumb=sectorPickerData.count
}
return returnNumb
}
//sets up the number of componenets in the picker view
func numberOfComponentsInPickerView(_pickerView: UIPickerView!) -> Int {
return 1
}
//sets up the titles for each row in the component
//changed "func PickerView to func pickerView to fix bug with "?" showing up instead of proper string in the picker view
func pickerView(_pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
var returnStr: NSString = "";
if (_pickerView == countryPicker)
{
returnStr=countryPickerData.objectAtIndex(row) as NSString
}
else if (_pickerView == sectorPicker){
returnStr=sectorPickerData.objectAtIndex(row) as NSString
}
return ""+returnStr
}
}