Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

64

CHAPTER 3: Handling Basic Interaction

Trying It Out

Guess what? We’re basically finished. Are you ready to try out our app? Let’s do it!

Select Product Run. If you run into any compile or link errors, go back and compare your code changes to those shown in this chapter. Once your code builds properly, Xcode will launch the iPhone simulator and run your application. When you tap the right button, the text “Right button pressed.” should appear (as in Figure 3–1). If you then tap the left button, the label will change to say “Left button pressed.”

Looking at the Application Delegate

Well, cool, your application works! Before we move on to our next topic, let’s take a minute to look through the two source code files we have not yet examined,

BIDAppDelegate.h and BIDAppDelegate.m. These files implement our application delegate.

Cocoa Touch makes extensive use of delegates, which are classes that take responsibility for doing certain tasks on behalf of another object. The application delegate lets us do things at certain predefined times on behalf of the UIApplication class. Every iOS application has one and only one instance of UIApplication, which is responsible for the application’s run loop and handles application-level functionality such as routing input to the appropriate controller class. UIApplication is a standard part of the UIKit, and it does its job mostly behind the scenes, so you generally don’t need to worry about it.

At certain well-defined times during an application’s execution, UIApplication will call specific methods on its delegate, if there is a delegate and that delegate implements the method. For example, if you have code that needs to fire just before your program quits, you would implement the method applicationWillTerminate: in your application delegate and put your termination code there. This type of delegation allows your application to implement common application-wide behavior without needing to subclass UIApplication or, indeed, without needing to know anything about the inner workings of UIApplication.

Click BIDAppDelegate.h in the project navigator to see the application delegate’s header file. It should look similar to this:

#import <UIKit/UIKit.h> @class BIDViewController;

@interface BIDAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) BIDViewController *viewController; @end

www.it-ebooks.info

CHAPTER 3: Handling Basic Interaction

65

One thing worth pointing out is this line of code:

@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>

Do you see that value between the angle brackets? This indicates that this class conforms to a protocol called UIApplicationDelegate. Hold down the option key. Your cursor should turn into crosshairs. Move your cursor so that it is over the word UIApplicationDelegate. Your cursor should turn into a pointing hand with a question mark in the center, and the word UIApplicationDelegate should be highlighted, as if it were a link in a browser (see Figure 3–14).

Figure 3–14. When you hold down the option key in Xcode and point at a symbol in your code, the symbol is highlighted and your cursor changes into a pointing hand with a question mark.

With the option key still held down, click this link. This will open a small popup window showing a brief overview of the UIApplicationDelegate protocol, as shown in Figure 3–15.

Figure 3–15. When we option-clicked <UIApplicationDelegate> from within our source code, Xcode popped up this window, called the Quick Help panel, which describes the protocol.

Notice the two icons in the upper-right corner of this new popup documentation window (see Figure 3–15). Click the left icon to view the full documentation for this symbol, or click the right icon to view the symbol’s definition in a header file. This same trick works with class, protocol, and category names, as well as method names displayed in the editor pane. Just option-click a word, and Xcode will search for that word in the documentation browser.

www.it-ebooks.info

66

CHAPTER 3: Handling Basic Interaction

Knowing how to quickly look up things in the documentation is definitely worthwhile, but looking at the definition of this protocol is perhaps more important. Here’s where you’ll find which methods the application delegate can implement and when those methods will be called. It’s probably worth your time to read over the descriptions of these methods.

NOTE: If you’ve worked with Objective-C before but not with Objective-C 2.0, you should be aware that protocols can now specify optional methods. UIApplicationDelegate contains many optional methods. However, you do not need to implement any of the optional methods in your application delegate unless you have a reason to do so.

Back in the project navigator, click BIDAppDelegate.m to see the implementation of the application delegate. It should look something like this:

#import "BIDAppDelegate.h"

#import "BIDViewController.h"

@implementation BIDAppDelegate

@synthesize window = _window;

@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[BIDViewController alloc]

initWithNibName:@"BIDViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible];

return YES;

}

-(void)applicationWillResignActive:(UIApplication *)application

{

/*

Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

*/

}

-(void)applicationDidEnterBackground:(UIApplication *)application

{

/*

Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

www.it-ebooks.info

CHAPTER 3: Handling Basic Interaction

67

If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

*/

}

-(void)applicationWillEnterForeground:(UIApplication *)application

{

/*

Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

*/

}

-(void)applicationDidBecomeActive:(UIApplication *)application

{

/*

Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

*/

}

- (void)applicationWillTerminate:(UIApplication *)application

{

/*

Called when the application is about to terminate. Save data if appropriate.

See also applicationDidEnterBackground:. */

}

@end

At the top of the file, you can see that our application delegate has implemented one of those protocol methods covered in the documentation, called application:didFinishLaunchingWithOptions:. As you can probably guess, this method fires as soon as the application has finished all the setup work and is ready to start interacting with the user.

Our delegate version of application:didFinishLaunchingWithOptions: creates a window, and then it creates an instance of our controller class by loading the nib file that contains our view. It then adds that controller’s view as a subview to the application’s window, which makes the view visible. This is how the view we designed is shown to the user. You don’t need to do anything to make this happen; it’s all part of the code generated by the template we used to build this project, but it’s good to know that it happens here.

We just wanted to give you a bit of background on application delegates and show how this all ties together before closing this chapter.

www.it-ebooks.info

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