Skip to content

Commit

Permalink
Picker working and saving the selected category
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyrz committed Jun 30, 2017
1 parent 894863d commit 7a74d75
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion iExpense/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
<constraint firstAttribute="width" constant="60" id="nXY-sE-srY"/>
</constraints>
</imageView>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="VOl-kA-Y2Q">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Category" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="VOl-kA-Y2Q">
<rect key="frame" x="60" y="0.0" width="283" height="60"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
Expand Down
6 changes: 5 additions & 1 deletion iExpense/NewExpenseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NewExpenseViewController: UIViewController, UIPickerViewDelegate, UIPicker
var realm : Realm!
var categoriesList : Results<CategoryDTO>!
var categoryPicker = UIPickerView()
var selectedCategory : CategoryDTO?

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -42,10 +43,12 @@ class NewExpenseViewController: UIViewController, UIPickerViewDelegate, UIPicker
}

@IBAction func saveAction(_ sender: Any) {
if (Int(self.valueInput.text!) != nil) {
if (Int(self.valueInput.text!) != nil && self.selectedCategory != nil) {
let myExpense = ExpenseDTO()
myExpense.value = Int(self.valueInput.text!)!
myExpense.desc = self.descriptionInput.text!
myExpense.category = self.selectedCategory

try! realm.write {
realm.add(myExpense)
}
Expand All @@ -69,6 +72,7 @@ class NewExpenseViewController: UIViewController, UIPickerViewDelegate, UIPicker

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.categoryInput.text = categoriesList.elements[row].name
self.selectedCategory = categoriesList.elements[row]
self.view.endEditing(false)
}
}

0 comments on commit 7a74d75

Please sign in to comment.