Building my first iOS app with Swift 2.0: Session III

After a good night’s sleep, it’s time to dive into the third session where I am building my first iOS app with Swift. Session I and Session II for reference.

I’ll start with some Auto Layout magic: it’s what you use to a get a consistent layout across different screen sizes. In the AddView, I used more control + drag magic to make sure the vertical spacing remains consistent on every screen size.

A useful tip while adding the layout constraints and setting Auto Layout options is to keep an eye out for warnings and errors. The “fix this for me” everything very well. It’s like the auto-correct for newbie Xcode developers.

Adding the constraints give you the ability to do things like set the height of the text box, the distance from the nearest element and horizontal spacing from the edges of the view to be consistent. I couldn’t figure out why after I added a few constraints, those kept disappearing from the little ‘Pin’ menu at the bottom.

All I can tell right now is these are the constraints I have added and I don’t know what each of those do.

I ran the simulator and check what happens on a couple of different iPhone types and rotated the screens around. It seems like it “constraints” the elements in terms of height and distance from nearest elements, but I am not confident if I have all the right constraints in place. Oh well, I think we will be coming back to these when testing.

To ‘edit’ these constraints, you can look at the ‘Sizing Inspector’ after selecting the element.

From what I can tell after loading the app and looking at the three screens on different iPhone versions, the view elements seem to be showing up fine. At this time, I don’t know what I don’t know, so will move along.

Next, we’ll get the DetailView to show the ‘details’ field for the Idea model.

First, create a class for the customcell that inherits from UITableViewCell. Make sure to bind this new customcell class to the customcell UI element in the Storyboard. Then in the customecell class I added a simple attribute called ‘Idea’ which is of type String to test linking the cell in the tableView of the ViewController. I assign the cell.Idea the textLabel that we have from the static array:

cell.Idea = cell.textLabel?.text

Sweet, now that I have the value in the cell, I will figure out how to pass it to the DetailView. Data is passed using segues.

I did a few things here:

  • I assigned an identifier called “detailview” to the segue between the ViewController and the DetailView.
  • In the ViewController, I added a prepareForSegue() function in which I check if(segue.identifier == “detailview”) and if so, I get the segue’s destinationViewController as the DetailViewConroller.
  • I also added a temporary string variable called ideaDetail to the DetailViewController. Back in the prepareForSegue(), I set the detailview.ideaDetail to cell.Idea (which is just the text string from the array).
  • Finally, in the DetailViewController, I can now add a method called viewDidAppear() in which I set the self.title to ideaDetail passed in the segue.
And, Voilà! The string of the Idea title from the table cells on the first screen shows up as a title in the DetailView. With that out of the way, next goal is to look for a cure for cancer.

It’s breakfast/lunch/brunch time, so let’s have this session end here. Everything pushed to GitHub.