Introduction
This is part of a series of articles describing the creation of a simple iOS app. A complete list of the articles is included in the first part A Simple Swift iOS App from Start to Finish - Introduction.
I have set up a web site for the finished app at http://DaysWithoutThings.com and you can download it free directly from the app store:
In this article I will begin implementing the code for the main view. I have set up a web site for the finished app at http://DaysWithoutThings.com and you can download it free directly from the app store:
The logic of the Main View
The main display view is loaded immediately after the launch screen. Its behavior will change depending on how many 'things' have been defined by the user.No Things
If no things have been defined it will open the List View immediately.One Thing
If only one thing has been defined it will display that thing and create a timer that will update the message once every hour. It does this because the app records the date and time of when a thing is created or reset. Therefore the count of whole days for a particular thing may change at any point during the day. The user can also tap the screen at any time to move to the list view.Several Things
If more than one thing is defined the main display will display each thing in turn changing to the next thing every 3 seconds. The user can swipe left or right to move to the previous or next things respectively. The user can also tap the screen at any time to move to the list view.Finding the number of 'things'
Before I can implement code for any of this logic I need to know how many 'things' are currently in the data store. To do this I need to give my MainDisplayViewController class access to the ThingCollection class I created in Creating the Collection Class.
I am actually just going to give my class a reference to the global AppDelegate object and use that to retrieve its ThingCollection object when I need it. To add the AppDelegate reference I will start by adding a constant to the class definition to hold the reference to the AppDelegate object.
class MainViewController: UIViewController {
let appDelegate:AppDelegate
This causes an error. When I click on the red error circle Xcode tells me I now need to create an init method for my class and offers to fix the problem by creating a stub for that method.
When I click to accept the fix Xcode inserts the stub at the top of my class definition.
When I click to accept the fix Xcode inserts the stub at the top of my class definition.
class MainViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appDelegate:AppDelegate
I need to add two things to this method. Firstly I need to assign the reference to the AppDelegate. Secondly I need to call the UIViewController's version of the same init method. Once I have done those two things I can remove the call to fatalError that Xcode inserted to remind me to implement the method. I am also going to move my definition of the constant back to the top of my class definition.
The logic to find and act on the number of things will be implemented in the viewWillAppear method. It will retrieve the number of things from the ThingCollection object and then execute a different method depending on how many there are:
The viewWillAppear method is an override of the UIViewController's default implementation. When I get the reference to my ThinCollection object I use the ! to unwrap the reference which allows me to use the count property directly in the switch statement.
class MainViewController: UIViewController {
let appDelegate:AppDelegate
required init(coder aDecoder: NSCoder) {
appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
super.init(coder: aDecoder)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
super.viewWillAppear(animated)
var thingCollection = appDelegate.thingCollection!
switch thingCollection.things.count {
case 0:
moveToList()
case 1:
displaySingleItem()
default:
displayMultipleItems()
}
}
func moveToList() {
}
func displaySingleItem() {
}
func displayMultipleItems() {
}
The viewWillAppear method is an override of the UIViewController's default implementation. When I get the reference to my ThinCollection object I use the ! to unwrap the reference which allows me to use the count property directly in the switch statement.
Implementing the logic for when there are no things
Obviously there are no 'things' in my data store yet as I have not implemented a way to add them. I could add some test records programmatically but I would rather implement the app in order of its workflow so in this article I can only implement the code for when there are no things defined.
I am going to do this by implementing a moveToList method which will move the app to the list view when there are no things defined. I created my List view and a custom class to control it in my previous article A Simple iOS App from Start to Finish - Creating the List View. All my moveTo List method will do is open is perform the segue to the list view that was created when I linked the list view to the tap gesture recognizer in my storyboard.
There are two steps to this. First I need to give that segue an identifier in the Storyboard so that I can reference it and then I just need to call the UIViewController's performSegueWithIdentifier method.
I will create the code for the displaySingleItem and DisplayMultipleItems in a future article once I have implemented a way to add new items.
There are two steps to this. First I need to give that segue an identifier in the Storyboard so that I can reference it and then I just need to call the UIViewController's performSegueWithIdentifier method.
func moveToList() {
performSegueWithIdentifier("ShowListView", sender: self)
}
Wrapping up
When I run my app now it appears to move directly from the launch screen to the list view.
In my next article I will start to implement the code for the list view that will allow me to add and remove things from the list.
Next: A Simple Swift iOS App from Start to Finish - Implementing the List View's code
Next: A Simple Swift iOS App from Start to Finish - Implementing the List View's code
No comments:
Post a Comment