Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

objective c - Possible to Subclass UILocalNotification and Change Default "Close" Button Text and Method?

Searching for a way to change the "Close" button text/functionality of a UILocalNotification...

I've found that it's impossible to access/call the text/function from another object, although subclassing UILocalNotification should allow implementation method overrides... not to mention the creation of an accessor to get/set the "Close" button text field.

What do you guys think about this? What would Apple?

Has anyone tried?...

EDIT: 12/21/2011 12:01 PM

The question that I'm asking involves an understanding of oop: late/early binding, dynamic method lookup, and declared type vs. runtime type field and method handling.

Subclassing of UILocalNotification does work...

UILocalNotificationExampleSubclass * example = [UILocalNotificationExampleSubclass init];

...and the device does create an object, however, with type UILocalNotification and not UILocalNotificationExampleSubclass.

I'm looking for insight into the UILocalNotification.m file's methods.

If it does not have its own methods, what object (name please) takes an instance of a UILocalNotification, uses its fields, and displays the object (name please) we see on screen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A UILocalNotification is just a storage for the notification's information. It does not perform anything.

Moreover your application does not display the notification. Another process does. So subclassing UILocalNotification is just useless.

EDIT at December 22nd, 17:53 UTC+1:

Yes, you can subclass UILocalNotification. But UILocalNotification is an abstract class and none of its properties is implemented. The alloc method is overridden so it returns an instance of UILocalNotification, a private subclass. That's why you cannot instantiate UILocalNotificationExampleSubclass.

But still, there is not point to subclass UILocalNotification because when you schedule a notification using -[UIApplication scheduleLocalNotification:] or present the notification immediately using -[UIApplication presentLocalNotification:], the operating system copies the notification.

That copy is stored in another process managed by the system, which uses its own private storage mechanism. A UILocalNotification is just a storage for a bunch of properties that is destined to get serialized and sent from the application to the operating system.

Now, we have that other process storing all the scheduled local notifications and waiting for a notification to fire. When that happens, that process will check if your application is in the foreground.

  • If your application is not in the foreground, that other process, which is totally out of our control, will create an alert and display the notification. We cannot customize that alert in any way, except by using the properties of the UILocalNotification class.
  • If your application is in the foreground, the notification will be sent back to the application that will create a new instance of UILocalNotification. Then, the UIApplication shared instance will access its delegate property and check if that delegate implements application:didReceiveLocalNotification:. If it does, you get the notification back and can do anything you want with that notification. For example, you may choose to display the notification using an alert view.

Configuring and displaying the alert view can be done like this:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertView *alertView =
    [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert", nil)
                               message:NSLocalizedString(notification.alertBody, nil)
                              delegate:nil
                     cancelButtonTitle:nil
                     otherButtonTitles:NSLocalizedString(@"OK", nil), nil];
    [alertView show];
    [alertView release]; // unless your project uses Automatic Reference Counting
}

I hope this longer response did answer your question, if what I'm saying is true.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...