mavriksc
mavriksc

Reputation: 1132

do you need to import .h files when using delegates?

-(IBAction)ok
{
    //send message to the delegate with the new settings
    [self.delegate setHeight:_height Width:_width Mines:_mines];
    [self.delegate dismissViewControllerAnimated:YES completion:nil];
}

the first message to the delegate wouldn't work until i imported ViewController.h, but the second one worked without the import. if i add -(void)setHeight:(int)h Width:(int)w Mines:(int)m; as required in the optionsViewController protocol will that mean that i no longer have to import the root .h file.

i intend to use delegation to send messages in other parts of the program so i want to make sure i am using it correctly and not importing things when i don't need to. Thank you.

Upvotes: 1

Views: 225

Answers (2)

Mario
Mario

Reputation: 4520

if i add -(void)setHeight:(int)h Width:(int)w Mines:(int)m; as required in the optionsViewController protocol will that mean that i no longer have to import the root .h file.

Yes! You could also add it as @optional and it would work (remember to check if the delegate -respondsToSelector: in that case). The whole idea is that your object regularly knows nothing about the delegate object - except that it conforms to the protocol (ie implements the @required and possibly the @optional methods).

Added for clarification (on my phone, which is a pain in the butt):

//OptionsViewController.h
//this object does NOT have to import
//the calling viewControllers .h file
//which is what I think the OP does

@protocol optionsViewControllerProtocol;

@interface OptionsViewController : UIViewController

@property (nonatomic, assign) id<optionsViewControllerProtocol> delegate; //should be id, could be UIViewController too, if absolutely necessary (better design to make it id) @end

@protocol optionsViewControllerProtocol <NSObject>

@required -(void) setHeight: (NSInteger) height; @end

//viewController.h #import "optionsViewController.h" //necessary to get the protocols definitions

@interface OptionsViewController: UIViewController <optionsViewControllerProtocol>

//.....

Upvotes: 1

MishieMoo
MishieMoo

Reputation: 6680

If you define your delegate property to be of class UIViewController*, then the compiler will recognize the dismissViewControllerAnimated:completion: method without you needing to import anything, since that's a standard method for that class.

For a custom method, i.e. setHeight:Width:Mines:, you absolutely need to import the header file, or have it imported somewhere up the import chain.

Example: You have MyProtocol.h, and you want SomeClass to have a delegate property that conforms to that protocol. If you #import "MyProtocol.h" in SomeClass.h, you don't need to re-import it in SomeClass.m.

// SomeClass.h
#import "MyProtocol.h"

@interface SomeClass : NSObject
@property (weak, nonatomic) id<MyProtocol> delegate;
@end

//SomeClass.m
#import "SomeClass.h"

@implementation SomeClass

- (void)someMethod
{
    [self.delegate myProtocolMethod];
}
@end

Upvotes: 0

Related Questions