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 )
object
Optionally specifies the source of the broadcast notification. If this parameter is
nil, notifications of the specified name will be received by the observer regardless
of which object broadcasts them. If this parameter is set, only the notifications of
the specified name that are broadcast by the given object will be observed.
In Recipe 1.29 we learned how to post notifications. Let's now try observing the notification that we learned to post there.
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
/* The notification name */
const NSString *ResultOfAppendingTwoStringsNotification =
@"ResultOfAppendingTwoStringsNotification";
/* Keys inside the dictionary that our notification sends */
const NSString
*ResultOfAppendingTwoStringsFirstStringInfoKey = @"firstString";
const NSString
*ResultOfAppendingTwoStringsSecondStringInfoKey = @"secondString";
const NSString
*ResultOfAppendingTwoStringsResultStringInfoKey = @"resultString";
- (void) broadcastNotification{
NSString *firstName = @"Anthony";
NSString *lastName = @"Robbins";
NSString *fullName = [firstName stringByAppendingString:lastName];
NSArray *objects = [[NSArray alloc] initWithObjects:
firstName,
lastName,
fullName,
nil];
NSArray *keys = [[NSArray alloc] initWithObjects:
ResultOfAppendingTwoStringsFirstStringInfoKey,
ResultOfAppendingTwoStringsSecondStringInfoKey,
ResultOfAppendingTwoStringsResultStringInfoKey,
nil];
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjects:objects
forKeys:keys];
NSNotification *notificationObject =
[NSNotification
notificationWithName:(NSString *)ResultOfAppendingTwoStringsNotification
object:self
1.30 Listening for Notifications Sent From NSNotificationCenter | 105
userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notificationObject];
}
- (void) appendingIsFinished:(NSNotification *)paramNotification{
NSLog(@"Notification is received.");
NSLog(@"Notification Object = %@", [paramNotification object]);
NSLog(@"Notification User-Info Dict = %@", [paramNotification userInfo]);
}
- (BOOL)
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
/* Listen for the notification */
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(appendingIsFinished:)
name:(NSString *)ResultOfAppendingTwoStringsNotification
object:self];
[self broadcastNotification];
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application{
/* We no longer observe ANY notifications */
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
When you run this app, you will see something similar to the following printed to the
console window:
Notification is received.
Notification Object =
Notification User-Info Dict = {
firstString = Anthony;
resultString = AnthonyRobbins;
secondString = Robbins;
}
As you can see, we are using the removeObserver: method of our notification center to
remove our object as an observer of all notifications. There are different was of removing
your objects from the chain of observers. Either you can quit cold-turkey, as we have
done here—that is, remove your object completely from observing any notification, or
106 | Chapter 1: The Basics
you can remove your object from observing specific notifications at specific times during the lifetime of your application. If you want to specify the notifications from which
you are removing your object from observing, simply call the removeOb
server:name:object: method of your notification center and specify the name of the
notification from which you are unsubscribing as well as (optionally) the object that
was sending the notifications.
See Also
XXX
1.30 Listening for Notifications Sent From NSNotificationCenter | 107
CHAPTER 2
Implementing Controllers and Views
2.0 Introduction
We write iOS applications using the MVC architecture. MVC is an abbreviation for
Model-View-Controller. These are the three main components of an iOS application
from an architectural perspective. The model is the brain of the application. It does the
calculations; it creates a virtual world for itself that can live without the views and
controllers. In other words, think of a model as a virtual copy of your application,
without a face!
Controllers in Xcode usually refer to view controllers. Think of view controllers as a
bridge between the model and your views. A view is the window through which your
users interact with your application. It displays what’s inside the model most of the
time, but in addition to that, it accepts users’ interactions. Any interaction between
the user and your application is sent to a view, which then can be captured by a view
controller and sent to the model.
In this chapter, you will learn how the structure of iOS applications is created and how
to use views and view controllers to create intuitive applications.
In this chapter, for most of the UI (User Interface) components that we
create, we are using a Single View Application template in Xcode. Follow
the instructions in Recipe 1.1 but instead of a Page-Based Application,
create a Single View Application. Make sure that your app is Universal,
as opposed to iPhone or an iPad app. A Universal app can run on both
iPhone and iPad.
109
2.1 Displaying Alerts with UIAlertView
Problem
You want to display a message to your users, in form of an alert to either ask them for
confirmation, ask for their username and password or simply ask them to enter a simple
text that you can use depending on your app's requirements.
Solution
Utilize UIAlertView.
Discussion
If you are an iOS user, you have most certainly already seen an alert view. Figure 2-1
depics an example of this:
110 | Chapter 2: Implementing Controllers and Views