-
-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathSelectorsFormViewController.swift
287 lines (225 loc) · 12.4 KB
/
SelectorsFormViewController.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
//
// SelectorsFormViewController.swift
// XLForm ( https://github.com/xmartlabs/XLForm )
//
// Copyright (c) 2014-2015 Xmartlabs ( http://xmartlabs.com )
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import MapKit
// Mark - NSValueTransformer
class NSArrayValueTrasformer : ValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSString.self
}
override class func allowsReverseTransformation() -> Bool {
return false
}
override func transformedValue(_ value: Any?) -> Any? {
if let arrayValue = value as? Array<AnyObject> {
return String(format: "%d Item%@", arrayValue.count, arrayValue.count > 1 ? "s" : "")
}
else if let stringValue = value as? String {
return String(format: "%@ - ) - Transformed", stringValue)
}
return nil
}
}
class ISOLanguageCodesValueTranformer : ValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSString.self
}
override class func allowsReverseTransformation() -> Bool {
return false
}
override func transformedValue(_ value: Any?) -> Any? {
if let stringValue = value as? String {
return (Locale.current as NSLocale).displayName(forKey: NSLocale.Key.languageCode, value: stringValue)
}
return nil
}
}
// Mark - SelectorsFormViewController
class SelectorsFormViewController : XLFormViewController {
fileprivate struct Tags {
static let Push = "selectorPush"
static let Popover = "selectorPopover"
static let ActionSheet = "selectorActionSheet"
static let AlertView = "selectorAlertView"
static let PickerView = "selectorPickerView"
static let Picker = "selectorPicker"
static let PickerViewInline = "selectorPickerViewInline"
static let MultipleSelector = "multipleSelector"
static let MultipleSelectorPopover = "multipleSelectorPopover"
static let DynamicSelectors = "dynamicSelectors"
static let CustomSelectors = "customSelectors"
static let SelectorWithSegueId = "selectorWithSegueId"
static let SelectorWithSegueClass = "selectorWithSegueClass"
static let SelectorWithNibName = "selectorWithNibName"
static let SelectorWithStoryboardId = "selectorWithStoryboardId"
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
initializeForm()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeForm()
}
override func viewDidLoad() {
super.viewDidLoad()
let barButton = UIBarButtonItem(title: "Disable", style: .plain, target: self, action: #selector(SelectorsFormViewController.disableEnable(_:)))
barButton.possibleTitles = Set(["Disable", "Enable"])
navigationItem.rightBarButtonItem = barButton
}
@objc func disableEnable(_ button : UIBarButtonItem)
{
form.isDisabled = !form.isDisabled
button.title = form.isDisabled ? "Enable" : "Disable"
tableView.endEditing(true)
tableView.reloadData()
}
func initializeForm() {
let form : XLFormDescriptor
var section : XLFormSectionDescriptor
var row : XLFormRowDescriptor
form = XLFormDescriptor(title: "Selectors")
section = XLFormSectionDescriptor.formSection(withTitle: "Selectors")
section.footerTitle = "SelectorsFormViewController.swift"
form.addFormSection(section)
// Selector Push
row = XLFormRowDescriptor(tag: Tags.Push, rowType:XLFormRowDescriptorTypeSelectorPush, title:"Push")
row.selectorOptions = [XLFormOptionsObject(value: 0, displayText: "Option 1")!,
XLFormOptionsObject(value: 1, displayText:"Option 2")!,
XLFormOptionsObject(value: 2, displayText:"Option 3")!,
XLFormOptionsObject(value: 3, displayText:"Option 4")!,
XLFormOptionsObject(value: 4, displayText:"Option 5")!
]
row.value = XLFormOptionsObject(value: 1, displayText:"Option 2")
section.addFormRow(row)
// Selector Popover
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad){
row = XLFormRowDescriptor(tag: Tags.Popover, rowType:XLFormRowDescriptorTypeSelectorPopover, title:"PopOver")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"]
row.value = "Option 2"
section.addFormRow(row)
}
// Selector Action Sheet
row = XLFormRowDescriptor(tag :Tags.ActionSheet, rowType:XLFormRowDescriptorTypeSelectorActionSheet, title:"Sheet")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
row.value = "Option 3"
section.addFormRow(row)
// Selector Alert View
row = XLFormRowDescriptor(tag: Tags.AlertView, rowType:XLFormRowDescriptorTypeSelectorAlertView, title:"Alert View")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
row.value = "Option 3"
section.addFormRow(row)
// Selector Picker View
row = XLFormRowDescriptor(tag: Tags.PickerView, rowType:XLFormRowDescriptorTypeSelectorPickerView, title:"Picker View")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
row.value = "Option 4"
section.addFormRow(row)
// --------- Fixed Controls
section = XLFormSectionDescriptor.formSection(withTitle: "Fixed Controls")
form.addFormSection(section)
row = XLFormRowDescriptor(tag: Tags.Picker, rowType:XLFormRowDescriptorTypePicker)
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
row.value = "Option 1"
section.addFormRow(row)
// --------- Inline Selectors
section = XLFormSectionDescriptor.formSection(withTitle: "Inline Selectors")
form.addFormSection(section)
row = XLFormRowDescriptor(tag: Tags.MultipleSelector, rowType:XLFormRowDescriptorTypeSelectorPickerViewInline, title:"Inline Picker View")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"]
row.value = "Option 6"
section.addFormRow(row)
// --------- MultipleSelector
section = XLFormSectionDescriptor.formSection(withTitle: "Multiple Selectors")
form.addFormSection(section)
row = XLFormRowDescriptor(tag: Tags.MultipleSelector, rowType:XLFormRowDescriptorTypeMultipleSelector, title:"Multiple Selector")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"]
row.value = ["Option 1", "Option 3", "Option 4", "Option 5", "Option 6"]
section.addFormRow(row)
// Multiple selector with value tranformer
row = XLFormRowDescriptor(tag: Tags.MultipleSelector, rowType:XLFormRowDescriptorTypeMultipleSelector, title:"Multiple Selector")
row.selectorOptions = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"]
row.value = ["Option 1", "Option 3", "Option 4", "Option 5", "Option 6"]
row.valueTransformer = NSArrayValueTrasformer.self
section.addFormRow(row)
// Language multiple selector
row = XLFormRowDescriptor(tag: Tags.MultipleSelector, rowType:XLFormRowDescriptorTypeMultipleSelector, title:"Multiple Selector")
row.selectorOptions = Locale.isoLanguageCodes
row.selectorTitle = "Languages"
row.valueTransformer = ISOLanguageCodesValueTranformer.self
row.value = Locale.preferredLanguages
section.addFormRow(row)
if UIDevice.current.userInterfaceIdiom == .pad {
// Language multiple selector popover
row = XLFormRowDescriptor(tag: Tags.MultipleSelectorPopover, rowType:XLFormRowDescriptorTypeMultipleSelectorPopover, title:"Multiple Selector PopOver")
row.selectorOptions = Locale.isoLanguageCodes
row.valueTransformer = ISOLanguageCodesValueTranformer.self
row.value = Locale.preferredLanguages
section.addFormRow(row)
}
// --------- Dynamic Selectors
section = XLFormSectionDescriptor.formSection(withTitle: "Dynamic Selectors")
form.addFormSection(section)
row = XLFormRowDescriptor(tag: Tags.DynamicSelectors, rowType:XLFormRowDescriptorTypeButton, title:"Dynamic Selectors")
row.action.viewControllerClass = DynamicSelectorsFormViewController.self
section.addFormRow(row)
// --------- Custom Selectors
section = XLFormSectionDescriptor.formSection(withTitle: "Custom Selectors")
form.addFormSection(section)
row = XLFormRowDescriptor(tag: Tags.CustomSelectors, rowType:XLFormRowDescriptorTypeButton, title:"Custom Selectors")
row.action.viewControllerClass = CustomSelectorsFormViewController.self
section.addFormRow(row)
// --------- Selector definition types
section = XLFormSectionDescriptor.formSection(withTitle: "Selectors")
form.addFormSection(section)
// selector with segue class
row = XLFormRowDescriptor(tag: Tags.SelectorWithSegueClass, rowType: XLFormRowDescriptorTypeSelectorPush, title: "Selector with Segue Class")
row.action.formSegueClass = NSClassFromString("UIStoryboardPushSegue")
row.action.viewControllerClass = MapViewController.self
row.valueTransformer = CLLocationValueTrasformer.self
row.value = CLLocation(latitude: -33, longitude: -56)
section.addFormRow(row)
// selector with SegueId
row = XLFormRowDescriptor(tag: Tags.SelectorWithSegueId, rowType: XLFormRowDescriptorTypeSelectorPush, title: "Selector with Segue Idenfifier")
row.action.formSegueIdentifier = "MapViewControllerSegue";
row.valueTransformer = CLLocationValueTrasformer.self
row.value = CLLocation(latitude: -33, longitude: -56)
section.addFormRow(row)
// selector using StoryboardId
row = XLFormRowDescriptor(tag: Tags.SelectorWithStoryboardId, rowType: XLFormRowDescriptorTypeSelectorPush, title: "Selector with StoryboardId")
row.action.viewControllerStoryboardId = "MapViewController";
row.valueTransformer = CLLocationValueTrasformer.self
row.value = CLLocation(latitude: -33, longitude: -56)
section.addFormRow(row)
// selector with NibName
row = XLFormRowDescriptor(tag: Tags.SelectorWithNibName, rowType: XLFormRowDescriptorTypeSelectorPush, title: "Selector with NibName")
row.action.viewControllerNibName = "MapViewController"
row.valueTransformer = CLLocationValueTrasformer.self
row.value = CLLocation(latitude: -33, longitude: -56)
section.addFormRow(row)
self.form = form
}
override func storyboard(forRow formRow: XLFormRowDescriptor!) -> UIStoryboard! {
return UIStoryboard(name: "iPhoneStoryboard", bundle:nil)
}
}