Building my first iOS app with Swift 2.0: Session IV

Rutul Davè
3 min readSep 30, 2015

After three sessions, I am now getting to the fun part. I will be creating the data model first and then connecting it into the view so that I can add and show non-static data.

I will keep the Idea model simple. There is an Idea.title and an Idea.Detail. To access the object instances, I will need AddIdea(), DeleteIdea() and GetIdea() methods that are handled by something called a IdeaManager.

Idea class
IdeaManager class

Now, we can connect these models to the view. I’ll do this in three parts in this session:

  1. Showing and hiding the keyboard
  2. Updating the button states
  3. Save

Showing and hiding the keyboard

Novice-tip: In your simulator, I needed to uncheck ‘Connect Hardware Keyboard’ to have the keyboard on the simulated phone show up when testing.

I need to do a couple of things:

  1. Make sure that when in the title UITextField, the keyboard is showing, the ‘Return Key’ is ‘Done’. I have to link and add a method that hides the keyboard when done is clicked. resignFirstResponder() is what hides the keyboard.
  2. For the larger UITextView for the ‘detail’ input for an Idea, I need to add a ‘Done’ button to the top navigation and then link it to a method that hides the keyboard when the ‘Done’ button is clicked.
Methods that hide the keyboard when the UITextField and UITextView are selected and ‘Done’ buttons are pressed.
Aiight, I now can do showing and hiding (mainly hiding) the keyboard.

Now, let’s make the buttons have state.

For the ‘Done’ button, I want to disable it until there is text in the UITextView for the detailContent. And I want to disable the ‘Add’ button until there is text in the UITextFiled and the UITextView.

So, I did a couple of things:

  1. Added a handleButtonStates() method that enables and disables the buttons depending on what’s in the input fields.
  2. Added an “observer” that observed the UITextField (titleText) and the UITextView (detailContent) and called handleButtonStates() to do it’s thing.
My buttons have state!
The ‘Done’ & ‘Add’ buttons are disabled until the text fields they are associated with are ready.

Now let’s see if we can save these text fields.

Side-note: I had to re-write the Idea and IdeaManager classes again as I might have dropped the local stashed changes on my dev system. Not important, but having the code described above helped me rewrite that code very quickly.

To save the text fields I did a few things:

  1. Added an array of Idea elements in IdeaManager
  2. Refactored the code so that everywhere I was using the simple ‘String’ variable for storing the title, now I am using the Idea class.
  3. Added an addButton_click() method that calls IdeaManager.AddIdea() and clears the text fields.
  4. In the main ViewController, updated the tableView method to use the ideas array from the IdeaManager
addButton_click that calls IdeaManager.AddIdea()
Updated tableView in ViewController to get the idea element from the ideas array in IdeaManager
My mom will be proud. Starting with an empty list, I can add an idea with a title and a detail field.

Boom! that was a productive session. I can now show and add non-static data. Code is on GitHub.



Rutul Davè

Co-founder & CTO at @ilovemaxwell. Previously founded @brightfunds.