是否可以子类化 UiApplicationDelegate 协议?
说实话我不知道怎么称呼它,所以我会尝试描述它。
UIApplicationDelegate 协议有“application:handleOpenURL:”方法。如果我在我的 ApplicationDelegate 类中实现这个方法,当有人打开我的 url 时就会调用它。
但是,我希望我的另一个类(uiviewcontroller)接收此调用。举一个不同的例子 - 您可以创建几个类,每个类都可以获得 GPS 位置。是否可以对 UIApplicationDelegate 协议执行相同的操作?
我在这里搜索了很多这个主题,但找不到任何关于如何做到这一点的答案。我知道如何获取我的应用程序委托([[UIApplication sharedApplication] delegate]),但在这种情况下并非如此。
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.
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 the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您对 Dan Ray 的回答的评论来看,听起来您正在寻找类似 Three20 基于 URL 的导航
Judging by your comments to Dan Ray's answer, it sounds like you are looking for something like Three20's URL-based navigation
最简单的解决方案是使用 NSNotification。这将允许您在任何需要的地方处理handleOpenURL调用,而不会在您的应用程序委托和您想要处理它的类之间创建任何不必要的耦合。
在您的应用程序委托中,处理委托方法并使用 NSNotificationCenter 转发数据。
现在,无论您在哪里需要处理此问题,只需注册为通知的观察者并从通知用户信息字典中提取 URL 即可。
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.
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.
你总是可以告诉那些从其他面向对象语言转向 Objective-C 的人,因为他们的第一本能就是子类、子类、子类。 obj-c 中没有太多子类化。显然,你可以,但这不是传统的完成方式,尤其是像 UIApplicationDelegate 这样一次性的事情。更 Cocoaish 的方式是使用类别,或者有时创建一个新的 NSObject 子类,其中包含可能的父类作为属性。
在这种情况下,子类化肯定是一个坏主意。 UIApplication 单例只能有一个委托属性。因此,如果您创建一个新的 UIApplicationDelegate,您就没有地方可以挂钩它。
相反,请优化您的一个委托的
application:handleOpenURL:
方法来捕获 URL 调用并加载要处理它的任何 UIViewController 子类(我知道,我知道:异常)。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.