Another thing we need to do before the application is ready for action is set up our view objects. This includes creating them, configuring them, and putting them on the screen. Makes sense, right? The user can’t tap a button that doesn’t exist or is not on the screen.
In iTahDoodle, you’re going to set up your views programmatically in application:didFinishLaunchingWithOptions:. There is also a visual “drag-and-drop” tool for setting up views that we’ll use in the next chapter.
I should warn you that here’s where the code starts getting dense. The detailed syntax of creating and showing views on the screen is a topic for a book specifically about iOS application programming. Try to follow the gist of what’s going on as you type in the code. You create each object and then configure it by setting some of its properties. Next, the configured view objects are added as subviews of the window object, and, finally, the window is placed on the screen.
#pragma mark - Application delegate callbacks - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Attempt to load an existing to-do dataset from an array stored to disk. NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()]; if (plist) { // If there was a dataset available, copy it into our instance variable. tasks = [plist mutableCopy]; } else { // Otherwise, just create an empty one to get us started. tasks = [[NSMutableArray alloc] init]; } // Create and configure the UIWindow instance // A CGRect is a struct with an origin (x,y) and size (width,height) CGRect windowFrame = [[UIScreen mainScreen] bounds]; UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame]; [self setWindow:theWindow]; // Define the frame rectangles of the three UI elements // CGRectMake() creates a CGRect from (x, y, width, height) CGRect tableFrame = CGRectMake(0, 80, 320, 380); CGRect fieldFrame = CGRectMake(20, 40, 200, 31); CGRect buttonFrame = CGRectMake(228, 40, 72, 31); // Create and configure the table view taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain]; [taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // Create and configure the text field where new tasks will be typed taskField = [[UITextField alloc] initWithFrame:fieldFrame]; [taskField setBorderStyle:UITextBorderStyleRoundedRect]; [taskField setPlaceholder:@"Type a task, tap Insert"]; // Create and configure a rounded rect Insert button insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [insertButton setFrame:buttonFrame]; // Buttons behave using a target/action callback // Configure the Insert button's action to call this object's -addTask: method [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside]; // Give the button a title [insertButton setTitle:@"Insert" forState:UIControlStateNormal]; // Add our three UI elements to the window [[self window] addSubview:taskTable]; [[self window] addSubview:taskField]; [[self window] addSubview:insertButton]; // Finalize the window and put it on the screen [[self window] setBackgroundColor:[UIColor whiteColor]]; [[self window] makeKeyAndVisible]; return YES; }
Now that you’ve set up your views, we can build the application to see how they look. In Xcode, find the Scheme dropdown menu near the Run button. Select iPhone 5.X Simulator for the latest version of the iOS simulator:
Build and run the application. You’ll get a warning from the compiler that you that you haven’t implemented addTask:. Ignore that for now; you’ll implement addTask: shortly.
The simulator lets you run Cocoa Touch applications on your desktop. This is a quick and easy way to see what your program should look and act like when it runs on an iOS device.
You can see the views that you set up and laid out in application:didFinishLaunchingWithOptions:, but they can’t do anything yet. In fact, tapping the Insert button will crash the application because the button’s action method, addTask:, isn’t implemented yet. (That’s one of the reasons the compiler warned you about it.)