Objective-C "private" protocols?

I have a view controller class (MyViewController) that deals with a subclass of UIView (MyView). I do not want any class other than the view controller class to know about the UIView subclass, so I cannot import MyView.hinto MyViewController.h.

So in MyViewController.m, I put

#import "MyViewController.h"
#import "MyView.h"

@interface MyViewController (PrivateObjects)

MyView *myView;

@end

...

However, to get feedback from MyView, I use a delegate. This delegate must implement the protocol MyViewDelegate.

How can I implement the protocol MyViewDelegateinternally MyViewControllerwithout having #import MyView.hin MyViewController.h?

+5
source share
5 answers
@interface MyViewController (PrivateObjects) <MyViewDelegate>
....

(, ivar .)

+14

forware MyViewController.h

@class MyView;

@interface MyViewController {
    MyView *myView;
}

@end
+2

, , , Cocoa.

, , .

, , C- , Objective-C , .

, IB , , , nib .

[ bbum ]

+1

(.h) MyView.h (.h) MyViewController, :

#import "MyViewDelegate.h"
@interface MyViewController : UIViewController <MyViewDelegate>

-

0
source

Why do you even want to use the protocol? Just put the methods in your PrivateObjects category.

Change . Apple refers to this technique as an “ unofficial protocol

-4
source

All Articles