当父类中有两个可能的委托时,在子类中设置委托

发布于 2024-09-14 19:55:20 字数 1829 浏览 1 评论 0原文

当我为 UIImagePickerController 设置委托时收到警告。这是因为 UIImagePickerController 及其父 UINavigationController 都有可以使用的委托。该代码工作正常,但只是想知道如何正确处理委托和继承并丢失警告。

所以基本上我创建了自己的 MyImagePickerController 它是 UIImagePickerController 的子类。 UIImagePickerController 是 UINavigationController 的子类。 所以继承树是

UINavigationController > UIImagePickerController > MyImagePickerController

MyImagePickerController 也是 UIImagePickerController 的委托。 所以我添加到我的@interface。

@interface MyImagePickerController : UIImagePickerController <UIImagePickerControllerDelegate>...

然后我在 loadView 中将 MyImagePickerController 设置为自身的委托

- (void) loadView
{
...
self.delegate = self;
...

} 我实现了 UIImagePickerControllerDelegate 委托方法并且一切正常。

但我收到警告

warning: class 'MyImagePickerController' does not implement the 'UINavigationControllerDelegate' protocol

问题是两个父类都有自己的委托

UINavigationController         > UIImagePickerController         > MyImagePickerController
UINavigationControllerDelegate > UIImagePickerControllerDelegate > MyImagePickerController

并且在 UIImagePickerController 的定义中委托方法可以采用 UINavigationControllerDelegate 或 UIImagePickerControllerDelegate

@interface UIImagePickerController : UINavigationController <NSCoding> {
@property(nonatomic,assign)    id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

应用程序工作,正在调用 MyImagePickerController 中的 UIImagePickerControllerDelegate 方法,但有没有正确的方法设置委托,以便阅读代码的人知道我们正在尝试实现 UIImagePickerControllerDelegate 方法,并让新手清除警告。

如果你想在 UIImagePickerControllerDelegate 中同时实现 UIImagePickerControllerDelegate 和 UINavigationControllerDelegate 方法,会发生什么?您将如何在 Obj-C 中设置单个委托? 干杯

I'm getting a warning when I set the delegate for UIImagePickerController. It's because UIImagePickerController and its parent UINavigationController both have delegates that can be used. The code works fine but just wondering how to handle delegates and inheritance properly and lose the warning.

So basically I've created my own MyImagePickerController which is subclass UIImagePickerController.
UIImagePickerController is a sub class of UINavigationController.
so the inheritance tree is

UINavigationController > UIImagePickerController > MyImagePickerController

MyImagePickerController is also the delegate for UIImagePickerController.
So I add to my @interface.

@interface MyImagePickerController : UIImagePickerController <UIImagePickerControllerDelegate>...

Then I set MyImagePickerController to be the delegate to itself in loadView

- (void) loadView
{
...
self.delegate = self;
...

}
And I implement the UIImagePickerControllerDelegate delegate methods and it all works.

But I get a warning

warning: class 'MyImagePickerController' does not implement the 'UINavigationControllerDelegate' protocol

The problem is that both parent classes have their own Delegates

UINavigationController         > UIImagePickerController         > MyImagePickerController
UINavigationControllerDelegate > UIImagePickerControllerDelegate > MyImagePickerController

And in the definition for UIImagePickerController the delegate method can take either UINavigationControllerDelegate or UIImagePickerControllerDelegate

@interface UIImagePickerController : UINavigationController <NSCoding> {
@property(nonatomic,assign)    id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

The app works, the UIImagePickerControllerDelegate methods in MyImagePickerController are being called but is there a proper way to set the delegate so someone reading the code knows we're trying to implement UIImagePickerControllerDelegate methods and for a newbie to clear the warning.

And what happens if you want to implement both UIImagePickerControllerDelegate and UINavigationControllerDelegate methods in the UIImagePickerControllerDelegate? How would you set the single delegate in Obj-C?
Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2024-09-21 19:55:20

阅读警告。它会告诉您您需要知道的一切。

警告:类
“MyImagePickerController”没有
实施
'UINavigationControllerDelegate'
协议

这一行:

@property(nonatomic,assign) iddelegate;

并不意味着委托对象(注意委托对象,而不是委托方法)可以是 UINavigationControllerDelegate 或 UIImagePickerControllerDelegate。这意味着委托对象必须是UINavigationControllerDelegate UIImagePickerControllerDelegate(它必须符合这两个协议)。

您似乎对子类化和继承有点困惑。问题不在于两个父类都有自己的委托。 UINavigationController 有一个委托属性 UINavigationControllerDelegate 并且因为 UIImagePickerController 是一个 UINavigationController 它也有这个委托,它仍然是 UINavigationControllerDelegate,但它还添加了额外的行为,正如子类往往会做的那样,委托现在不仅必须实现 UINavigationControllerDelegate 方法,而且还必须实现 UIImagePickerControllerDelegate 方法。

所以问题不在于“如果你想同时实现这两者会发生什么……”。你必须同时实现两者。

Read the warning. It tells you everything you need to know.

warning: class
'MyImagePickerController' does not
implement the
'UINavigationControllerDelegate'
protocol

This Line:

@property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

Does not mean that the delegate object (Note delegate object, not delegate method) can be a UINavigationControllerDelegate or a UIImagePickerControllerDelegate. It means that the delegate object must be a UINavigationControllerDelegate and a UIImagePickerControllerDelegate (it must conform to both these protocols).

You seem to be slightly confused about subclassing and inheritance. The problem isn't that both parent classes have their own Delegates. UINavigationController has a delegate property UINavigationControllerDelegate and because UIImagePickerController is a UINavigationController it also has this delegate, which is still UINavigationControllerDelegate, but it has also added extra behaviour, as subclasses tend to do, and the delegate must now not only implement UINavigationControllerDelegate methods but also UIImagePickerControllerDelegate methods.

So the problem isn't "what happens if you want to implement both..". You have to implement both.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文