Mobile App Development for iOS
Notes Topics Weekly Resources Graded work Professor Code examplesLast week, we introduced a large number of topics that will help you build iOS apps. We did get started on some interaction foundations, by getting content to-and-from the view and the code.
This week, we continue learning some more interaction essentials.
To review last week’s coverage, here are the interaction foundations we learned:
Doing all this enabled you to learn:
This week, we’ll go further, and quickly. Here’s what’s planned:
Here are a few notes or discussion points that will guide our learning journey.
Consider a simple view with three UI objects:
It seems pretty easy to get started with the task of getting input from the user (via the text field) and displaying the results (via the label). For example, here’s the code in the body of a view controller that implements the barely-essential and therefore incomplete code:
// Variables
// Outlets
@IBOutlet weak var incomingText: UITextField!
@IBOutlet weak var textResult: UILabel!
// Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
// Actions
@IBAction func updateView(_ sender: UIButton) {
// Optional... transform to upper case
textResult.text = incomingText.text?.uppercased()
// Clear the incoming text field
incomingText.text = ""
}
The problem is that it does not handle the keyboard properly.
We want the keyboard to disappear after the user taps the button. So, we’ll learn how.
Then we’ll go deeper, with a more interesting example, which will enable us to introduce and disuss the delegation topic.
Be prepared to listen, think, learn, question, then apply this new knowledge. Students who are familiar with web apps might think that iOS apps work the same, but no, they don’t.
The main take-away is that the code we write does NOT control the logic flow and workflow in the app.
Instead, we design the interaction pattern (before we write any code!), and then create and code program objects (classes etc.) that react to user interaction and events that happen on the device. We’re just writing a bunch of event handlers.
The iOS runtime is in control.
Another interesting difference, when compared to web apps, is that our app is stateful when loaded. As we create objects, they remain in memory, and continue to be available, until they are deliberately destroyed, or the app is terminated.
What does delegate or delegation mean in the real world? Google results will remind you. When people are involved, a delegate will perform a task or duty at the request of someone. That “someone” is delegating the task to a delegate.
In iOS, a delegate is an object that performs a task at the request of another object. As above, that “other object” is delegating the task to a delegate.
The delegator is often - but not always - a built-in iOS SDK (framework) object. For example, a text field (UITextField). We do not have access to the source code of UITextField, so we cannot write custom code that handles events from it. The solution is to configure a delegate - often a controller in our app - with the custom code.
There is a standard technique for declaring/identifying/configuring the delegate. And, there’s a standard technique for writing code in the delegate to handle events.
Where do we see delegation in our app? While it’s incorrect to say “everywhere”, you will find that it is almost everywhere. Some UI objects depend on delegation to work. And, we’ll soon see the value of delegation to build that in to our app design (and logic and work flows).
Today, we are introduced to the idea of a “data model object”.
Today, we will configure the data model inside a view controller. In the near future, we will create a custom data manager class that will serve the needs of ALL view controllers in the app.
One way of selecting an item from a collection.
A common way of interacting with a collection of data.
How can you make this kind of app? Here are some instructions - try them. Soon, there will be a project template available, but not before your Assignment 1 is due.
tableView(_:cellForRowAtIndexPath:)
method; and replace the “reuseIdentifier” string with “default” (the same string as in the previous step)