Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.67 MB, 89 trang )
Figure 1-35. Placing an image inside the bundle which we created before
Since the Resources.bundle is added to our app's main bundle, we will need to use the
main bundle in order to find the path to our Resources.bundle. Once that is done, we
can directly access the files (only AlanSugar.png right now) inside this bundle. Since
bundles other than the main bundle can have folders embedded inside them, to access
files inside folders of a bundle other than the main bundle it is best to use the pathFor
Resource:ofType:inDirectory: method of NSBundle to explicitly specify the folder in
which a specific file/resource exists.
- (BOOL)
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSString *resourcesBundlePath =
[[NSBundle mainBundle] pathForResource:@"Resources"
ofType:@"bundle"];
if ([resourcesBundlePath length] > 0){
NSBundle *resourcesBundle = [NSBundle bundleWithPath:resourcesBundlePath];
if (resourcesBundle != nil){
NSString *pathToAlanSugarImage =
[resourcesBundle pathForResource:@"AlanSugar"
ofType:@"png"
inDirectory:@"Images"];
if ([pathToAlanSugarImage length] > 0){
98 | Chapter 1: The Basics
UIImage *image = [UIImage imageWithContentsOfFile:pathToAlanSugarImage];
if (image != nil){
NSLog(@"Successfully loaded the image from the bundle.");
} else {
NSLog(@"Failed to load the image.");
}
} else {
NSLog(@"Failed to find the file inside the bundle.");
}
} else {
NSLog(@"Failed to load the bundle.");
}
} else {
NSLog(@"Could not find the bundle.");
}
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
If you are attempting to find all the resources which are stored in a specific folder inside
a bundle, you can use the pathsForResourcesOfType:inDirectory: method of the NSBun
dle class. In this code, we will attempt to find the path to all the .png files inside the
Images folder of our Resources.bundle bundle:
- (BOOL)
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSString *resourcesBundlePath =
[[NSBundle mainBundle] pathForResource:@"Resources"
ofType:@"bundle"];
if ([resourcesBundlePath length] > 0){
NSBundle *resourcesBundle = [NSBundle bundleWithPath:resourcesBundlePath];
if (resourcesBundle != nil){
NSArray *PNGPaths = [resourcesBundle pathsForResourcesOfType:@"png"
inDirectory:@"images"];
[PNGPaths
enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Path %lu = %@", (unsigned long)idx+1, obj);
}];
} else {
1.28 Loading Data From Other Bundles | 99
}
NSLog(@"Failed to load the bundle.");
} else {
NSLog(@"Could not find the bundle.");
}
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The enumerateObjectsUsingBlock: method of NSArray accepts a block
object as its parameter. For more information about enumerateObject
sUsingBlock: and the block object it accepts, please refer to Recipe 1.25.
See Also
XXX
1.29 Sending Notifications with NSNotificationCenter
Problem
You want to broadcast an event in your app and allow any object that is willing to listen
to it to take action, depending on the notification that you are broadcasting.
Solution
Use the postNotificationName:object:userInfo: method of the default notification
center of type NSNotificationCenter to post a notification that carries an object (usually
the object that fires the notification) and a user-info dictionary that can carry extra
information about the notification and/or the object that fires the notification.
Discussion
Notification centers are dispatch centrals for notification objects. For instance, when the
keyboard pops up anywhere while the user is inside your app, iOS will send a notification to your app. Any object inside your app willing to listen to this notification can
add itself to the default notification center as an observer for that particular notification.
Once your object's lifetime comes to an end, it must remove itself from the notification
center's dispatch table. As a result, a notification is a message that gets broadcasted to
observers through a notification center. A notification center is an instance of NSNoti
100 | Chapter 1: The Basics
ficationCenter class. We retrieve the default notification center object using the
defaultCenter class method of NSNotificationCenter.
Notifications are objects of type NSNotification. A notification object has a name
(specified as NSString) and can carry two key pieces of information:
You can specify the name of your notifications yourself. You don't have
to use an API for that. Just make sure that your notification names are
unique enough that they won't clash with a system notification.
Sender Object
This is the instance of the object that fires the notification. The observer can access
this object using the object instance methof of the NSNotification class.
User-Info Dictionary
This is an optional dictionary that the sender object can create and send alongside
a notification object. This dictionary usually contains more information about the
notification. For instance, when a keyboard is about to get displayed in iOS for any
component inside your app, iOS sends the UIKeyboardWillShowNotification notification to the default notification center. The user-info dictionary of this notification contains values such as the rectangle of the keyboard before and after animation and the animation duration of the keyboard. Using this data, an observer
can make a decision as to, for instance, what to do with UI components that potentially will be obstructed once the keyboard gets displayed on the screen.
Notifications are a great way of implementing decoupled code. By that
I mean, using notifications, you can get rid of completion handlers and
delegation. However, there is one potential caveat about notifications:
they are not delivered immediately. They are dispatched by notification
centers, and the implementation of NSNotificationCenter is hidden
from application programmers. Delivery might sometimes be delayed
by a few milliseconds or in extreme cases (which I have never encountered) a few seconds. As a result, it is up to you to decide where to and
where not to use notifications.
In order to construct a notification of type NSNotification, use the notificationWith
Name:object:userInfo: class method of NSNotification like so:
It is best to suffix your notification names with the word Notification.
For instance, it is permitted to give your notification a name similar to
ResultOfAppendingTwoStrings. However, it is better to give the name
ResultOfAppendingTwoStringsNotification, as that clearly says what
this name belongs to.
1.29 Sending Notifications with NSNotificationCenter | 101