One thing that you need to do before the application is ready for the user is set up your view objects: the text field, the button, and the table view. This means creating them, configuring them, and putting them on the screen.
In iTahDoodle, you are going to set up your views programmatically in application:didFinishLaunchingWithOptions:. Xcode also has a visual “drag-and-drop” tool for setting up views, which you will use in Chapter 32.
The code for creating and configuring views is dense, and we are not going to cover it in detail. The detailed syntax of creating and showing views on the screen is a topic for a book specifically about iOS application programming.
Still, you can follow the gist of what is happening. First, 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.
In BNRAppDelegate.m, remove any boilerplate code from application:didFinishLaunchingWithOptions: and replace it with the following code:
#pragma mark - Application delegate callbacks - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Create and configure the UIWindow instance // A CGRect is a struct with an origin (x,y) and a size (width,height) CGRect winFrame = [[UIScreen mainScreen] bounds]; UIWindow *theWindow = [[UIWindow alloc] initWithFrame:winFrame]; self.window = theWindow; // Define the frame rectangles of the three UI elements // CGRectMake() creates a CGRect from (x, y, width, height) CGRect tableFrame = CGRectMake(0, 80, winFrame.size.width, winFrame.size.height - 100); CGRect fieldFrame = CGRectMake(20, 40, 200, 31); CGRect buttonFrame = CGRectMake(228, 40, 72, 31); // Create and configure the UITableView instance self.taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain]; self.taskTable.separatorStyle = UITableViewCellSeparatorStyleNone; // Tell the table view which class to instantiate whenever it // needs to create a new cell [self.taskTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; // Create and configure the UITextField instance where new tasks will be entered self.taskField = [[UITextField alloc] initWithFrame:fieldFrame]; self.taskField.borderStyle = UITextBorderStyleRoundedRect; self.taskField.placeholder = @"Type a task, tap Insert"; // Create and configure the UIButton instance self.insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.insertButton.frame = buttonFrame; // Give the button a title [self.insertButton setTitle:@"Insert" forState:UIControlStateNormal]; // Add our three UI elements to the window [self.window addSubview:self.taskTable]; [self.window addSubview:self.taskField]; [self.window addSubview:self.insertButton]; // Finalize the window and put it on the screen self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
Now that you have set up your views, you can run iTahDoodle on the iOS simulator to see them.