iOS - Coding and launching the application

To complete the iOS application, I'll add some basic code for creating two FishingSpot instances, and displaying them in a label. When launched, the application will look as follows:

To start out, each iOS application has some basic scaffolding. In this example, we have the following:

  • I've created a group named 'model' to store the basic business objects of the application. (Namely, FishingSpot.h and FishingSpot.m)
  • Each iOS application has an AppDelegate.h and AppDelegate.m file. This is the main entry point into the code.
  • For this application, we have a ViewController that's used to display the contents of a view. There are four files associated with the controller: ViewController.h, ViewController.m, ViewController_iPhone.xib, and ViewController_iPad.xib

(Note: For simplicity, each of the header and implementation files are combined. In practice, these are two separate files.)

The Application Delegate Files

  1. // -------------------------- AppDelegate.h -----------------------------------
  2. #import <UIKit/UIKit.h>
  3.  
  4. @class ViewController;
  5.  
  6. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  7.  
  8. @property (strong, nonatomic) UIWindow *window;
  9. @property (strong, nonatomic) ViewController *viewController;
  10.  
  11. @end
  12.  
  13. // ------------------------- AppDelegate.m -------------------------------------
  14.  
  15.  
  16. #import "AppDelegate.h"
  17.  
  18. #import "ViewController.h"
  19.  
  20. @implementation AppDelegate
  21.  
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  23. {
  24. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  25. // Override point for customization after application launch.
  26. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  27. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
  28. } else {
  29. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
  30. }
  31. self.window.rootViewController = self.viewController;
  32. [self.window makeKeyAndVisible];
  33. return YES;
  34. }
  35.  
  36. @end

These files are standard "boilerplate" code for launching an iOS application. The key item to note at this point is the didFinishLaunchingWithOptions method. Based on the type of device - iPhone or iPad - a ViewController is instantiated and initialized with the appropriate "xib" file. (A .xib file - pronounced "zib" - is where the layout of the UI occurs.) In this case, the view controller is instantiated, initialized, and set as the root view controller. The window is made visible, and the view is displayed.

The ViewController

  1. // ------------------------- ViewController.h -------------------------------------
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5. @interface ViewController : UIViewController {
  6.  
  7. __weak IBOutlet UILabel *displayText;
  8. }
  9.  
  10. @end
  11.  
  12. // ------------------------- ViewController.m -------------------------------------
  13.  
  14.  
  15. #import "ViewController.h"
  16. #import "FishingSpot.h"
  17.  
  18. @implementation ViewController {
  19. FishingSpot *spot1;
  20. FishingSpot *spot2;
  21. }
  22.  
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view, typically from a nib.
  27. spot1 = [[FishingSpot alloc] init];
  28. spot2 = [[FishingSpot alloc] initWithName:@"I3 Reef" atLat:27.315044 andLng:-82.593842];
  29. spot2.comments = @"Inshore Fishing";
  30. [displayText setText:[NSString stringWithFormat:@"Spot1: %@\r\n\r\nSpot2: %@", spot1, spot2]];
  31. }
  32.  
  33.  
  34. @end

The displayText variable will be described below. At this point, it's sufficient to note that it's a pointer to the label defined in the .xib file.

In the implementation file, there's a key method: viewDidLoad. In it, I create two FishingSpot instances, and then fill the label with the description of the instances. (See above for the output on the screen.)

Also note that the NSString class has a series of powerful string formatters; if you're experienced with C programming, these will look familiar.

So how did the displayText variable get initialized? It was done in the iOS development environment - XCode:

Within the phone layout, I placed a label. I then opened the ViewController.h file in a secondary window. (Option + click on that file to open it in a second window.) Once opened, I dragged the label into the header file, and named the variable. Once this is done, XCode (and iOS) takes care of linking the variable to the actual control.