Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
14
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

Chapter 27 Your First iOS Application

// Otherwise, just create an empty one to get us started. tasks = [[NSMutableArray alloc] init];

}

}

Wondering about the #pragma mark at the start of this code? Objective-C programmers often use this construct to group their methods within a class. Xcode knows about it, too. On the navigation bar at the top of the editor, find the item to the right of BNRAppDelegate.m. (Right now, this item probably reads @implementation AppDelegate, but it depends on where your cursor is in the code.) Click on this item, and Xcode will show you a list of locations in this file. You can click on any of these to be taken to that location in the code. Notice that your new pragma mark shows up on this list. This is very handy when you have many methods in a class.

Setting up views

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);

184

Running on the iOS simulator

// 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;

}

Running on the iOS simulator

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:

Figure 27.6 Scheme selector

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.

185

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]