开发者

Is it possible to subclass UiApplicationDelegate protocol?

To be honest I don't know how to call it, so I'll try to describe it.

UIApplicationDelegate protocol has "application:handleOpenURL:" method. And if I implement this method in my ApplicationDelegate class, it will be called when somebody opens my urls.

details: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleOpenURL:

However, I'd like my other class (uiviewcontroller) to receive this call. To make a different example - you can create a few classes and each of them can get GPS position. Is it possible to do t开发者_Python百科he same with UIApplicationDelegate protocol?

I searched a lot for this topic here, but I couldn't find any answer on how to do it. I know how to get my application delegate ([[UIApplication sharedApplication] delegate]), but it's not the case in this situation.


You can always tell somebody who came to objective-c from some other object oriented language, because their first instinct is to subclass, subclass, subclass. There's not a lot of subclassing in obj-c. You CAN, obviously, but it's not how things are conventionally done, especially with things that are as one-shot-ish as UIApplicationDelegate. The more Cocoaish Way is to use categories, or sometimes to create a new NSObject subclass that contains the would-be parent class as a property.

In this case, for sure subclassing is a bad idea. the UIApplication singleton can only have one delegate property. So if you create a new UIApplicationDelegate, you've got no place to hook to it.

Instead, smarten up your one delegate's application:handleOpenURL: method to catch the URL call and load up whichever UIViewController subclass (I know, I know: exceptions) is going to handle it.


The simplest solution would be to use an NSNotification. This will allow you to handle the handleOpenURL call wherever you need to without creating any unnecessary coupling between your application delegate and the class you want to handle it.

In your app delegate, handle the delegate method and forward the data on using NSNotificationCenter.

- (void)application:(UIApplication *)application handleOpenURL:(NSURL *)URL
{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyApplicationHandleOpenURLNotification" object:self userInfo:[NSDictionary dictionaryWithObject:URL forKey:@"URL"]];
}

Now, wherever you need to handle this, simply register as an observer for the notification and pull the URL out of the notification user info dictionary.


Judging by your comments to Dan Ray's answer, it sounds like you are looking for something like Three20's URL-based navigation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜