Properly declare delegation in Objective C (iPhone) - iphone

Ok, This has been explained a few times (I got most of the way there using this post on SO), but I am missing something. I am able to compile cleanly, and able to set the delegate as well as call methods from the delegate, but I'm getting a warning on build:
No definition of protocol 'DetailViewControllerDelegate' is found
I have a DetailViewController and a RootViewController only. I am calling a method in RootViewController from DetailViewController. I have the delegate set up as so:
In RootViewController.h:
#import "DetailViewController.h"
#interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate, DetailViewControllerDelegate> //Error shows up here
{
//Some Stuff Here
}
//Some other stuff here
#end
In RootViewController.m I define the delegate when I create the view using detailViewController.delegate = self
In DetailViewController.h:
#protocol DetailViewControllerDelegate;
#import "RootViewController.h"
#interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
#protocol DetailViewControllerDelegate
//some methods that reside in RootViewController.m
#end
I feel weird about declaring the protocol above the import in DetailViewController.h, but if I don't it doesn't build. Like I said, the methods are called fine, and there are no other errors going on. What am I missing here?

pheelicks is pretty much there but it looks like some of your protocol methods also use the DetailViewController class, I imagine it looks something like this :
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end
#class DetailViewController : UITableViewController <UITextFieldDelegate> {
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
and you haven't defined DetailViewController yet so you will get an error in the protocol definition.
You can fix this in two ways :
a) Declare (but don't define yet) the class before the protocol
#class DetailViewController;
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end
b) Just use UITableViewController instead of DetailViewController in your protocol methods.
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(UITableViewController *)controller hasSomething:(id)thing;
#end
Personally, I choose solution (a) but it really depends on what you're trying to do.
Hope that helps.

Try:
In DetailViewController.h:
#import "RootViewController.h"
#protocol DetailViewControllerDelegate <NSObject>
//some methods that reside in RootViewController.m
#end
#interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end

Here is another way that you could tackle this, similar to the solution proposed by deanWombourne.
#protocol DetailViewControllerDelegate;
#interface DetailViewController : UITableViewController <UITextFieldDelegate> {
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end

Related

Declare delegate when using ARC

In fastpdfkit has delegate declaration like this
#interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
//Delegate to get the current page and tell to show a certain page. It can also be used to
// get a list of bookmarks for the current document.
NSObject<BookmarkViewControllerDelegate> *delegate;
}
#property (nonatomic, assign) NSObject<BookmarkViewControllerDelegate> *delegate;
#synthesize delegate;
As i m using ARC so declaration of delegate is like this
#interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
id __unsafe_unretained <BookmarkViewControllerDelegate> delegate;
}
#property (unsafe_unretained) id <BookmarkViewControllerDelegate> delegate;
#synthesize delegate;
Is it correct cause when i m debugging i m getting
currentPage NSUInteger 0
delegate objc_object * 0x00000000
Heres a pattern to follow. The delegate is properly declared weak (an object but with no transfer of ownership or increase in retain count).
#protocol MyClassDelegate;
#interface MyClass : NSObject
#property(weak, nonatomic) id<MyClassDelegate>delegate;
#end
#protocol MyClassDelegate <NSObject>
- (void)myClass:(MyClass *)myClass didSomethingAtTime:(NSDate *)time;
- (CGSize)sizeofSomethingNeededByMyClass:(MyClass *)myClass;
// and so on
#end

Defining delegate function in separate file (instead of in ViewController)

I have a ViewController, and a UIView.
The UIView has a delegate, and the delegate function is set in the ViewController.
All I want to do, is have the delegate function defined in a separate file. So the UIView.m #imports the separate file, instead of all the ViewControllers which use the UIView.
I believe this is a standard procedure, but keep falling over myself trying to get it to work. :| Would really appreciate some help. Thanks.
myViewController.h
#import <UIKit/UIKit.h>
#import "myUIView.h"
#protocol ModalViewDelegate
-(void)didReceiveMessage:(NSString *)message;
#end
#interface myViewController : UIViewController <ModalViewDelegate>
#property (nonatomic, strong) myUIView *myUIViewItem;
#end
myViewController.m
#import "myViewController.h"
#import "myUIView.h"
#interface myViewController ()
#end
#implementation myViewController
#synthesize myUIViewItem;
- (void)didReceiveMessage:(NSString *)message { //<<< THIS IS WHAT
NSLog(#"Message from button: %#", message); //<<< NEEDS MOVING
}
- (void)viewDidLoad
{
…
myUIViewItem.delegate = self;
…
myUIView.h
#import <UIKit/UIKit.h>
#protocol ModalViewDelegate;
#interface myUIView : UIView {
id<ModalViewDelegate> delegate;
}
#property (nonatomic, strong) id<ModalViewDelegate> delegate;
myUIView.m
#import "myUIView.h"
#import "myViewController.h"
#implementation myUIView
#synthesize delegate;
...
[delegate didReceiveMessage:#"Data from UIView!"];
well, there is one method actually,
Just take one .h file and lets say connectionDelegate.h and declare your protocol init
In connectionDelegate.h:
#import <UIKit/UIKit.h>
#protocol ConnectionDelegate
-(void)getResult:(NSString*)_result;
#end
Then in your view controller:
#import "ConnectionDelegate.h"
#interface myViewController : UIViewController <ConnectionDelegate>
{
id delegate;
}
then in .m file, by just call the method
[delegate getResult:_result];
Edit regarding the warnings:
You need to declare the method in view controller, you need to do like this.
[self getResult:urlDataString];
-(void)getResult:(NSString*)_result{
[delegate getResult:_result];
}
Based on your comment:
I want to have the function 'didReceiveMessage' defined in a SEPARATE
file. So that I don't have to repeat it in every ViewController that
uses the UIView and delegate. e.g. ModalViewDelegate_Action.h and
ModalViewDelegate_Action.m
The way I was given was to use a subclass, and that's been working great for me. In my iOS projects I have a class called BaseViewController, which is a subclass of UIViewController. I put lots of code in it related to HUD management, NSOperations management, etc. Then virtually all my view controllers are subclasses of it.

Protocol declaration warning

MPPopoverControllerDelegate.h file
#import <Foundation/Foundation.h>
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
MPPopoverController.h file
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController <MPPopoverControllerDelegate>
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
MPPopoverController.m file
#import "MPPopoverController.h"
#import "MPPopoverControllerDelegate.h"
#implementation MPPopoverController
#end
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate; : this line has warning
Cannot find protocol definition for 'MPPopoverControllerDelegate'
What is wrong? And how to fix this warning?
if replace '#protocol MPPopoverControllerDelegate'; with '#import "MPPopoverControllerDelegate.h', everything will be ok. But link - in Referring to Other Protocols you can see that apple says to use #protocol
Is there an absolute need for your protocol declaration to be in a different header file? Unless it's quite a large protocol definition (which yours isn't), I would suggest declaring it below your interface declaration.
MPPopoverController.h
#import <UIKit/UIKit.h>
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
Compiler read your .m file, and load .h files when necessary. So it loads MPPopoverController.h first and when it read it delegate protocol is still undeclared. You could easily fix this warning just by swapping include lines. To let compiler read delegate .h file first.
#import "MPPopoverControllerDelegate.h"
#import "MPPopoverController.h"
#implementation MPPopoverController
#end
Are you sure you're ever including MPPopoverControllerDelegate.h somewhere?
Import MPPopoverControllerDelegate.h in MPPopoverController.h and MPPopoverController.h should look like:
The code copy pasted from the question and edited was removed. The following code is copy pasted from xcode.
The MPPopoverControllerDelegate.h:
#class MPPopoverController;
#protocol MPPopoverControllerDelegate <NSObject>
#optional
- (void)popoverControllerDidDismissPopover:(MPPopoverController *)popoverController;
#end
The MPPopoverController.h
#protocol MPPopoverControllerDelegate;
#interface MPPopoverController : UIViewController{
id<MPPopoverControllerDelegate> delegate;
}
#property (nonatomic, assign) id<MPPopoverControllerDelegate> delegate;
#end
The MPPopoverController.m
#implementation MPPopoverController
#synthesize delegate;
//rest of view controller class.
The problem is that in your MPPopoverController interface you specify <MPPopoverControllerDelegate>. This means that the class implements this protocol! Is wrong because the class is the owner of the protocol. So your logic is wrong in some point.

iPhone - class stop seeing superclass' reference

I have this class where I have something like this on .h
#interface myClass : UIImageView <UIGestureRecognizerDelegate>{
#public id referenceOne;
#public id referenceTwo;
}
#property (nonatomic,retain) id referenceOne;
#property (nonatomic,retain) id referenceTwo;
on .m I have
#synthesize referenceOne, referenceTwo;
This class has no delegate protocol.
I have other classes that are based on this one. For one of these classes I have defined a delegate protocol and have my implementation file like this:
#protocol MyBasedClassDelegate <NSObject>
#optional
- (void) doStuff;
#end
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> _delegate;
}
#property(nonatomic,assign) id<MyBasedClassDelegate> delegate;
and on .m I have
#synthesize delegate;
as soon as I have defined this MyBasedClassDelegate the class stopped seeing the referenceOne and referenceTwo ids inherited from myClass. Now Xcode says these are not declared. If I disable the protocol, it sees the references again.
Why is that and how do I solve that?
thanks.
You have an error here:
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> _delegate;
}
rename to
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> delegate;
}
OR
do #synthesize delegate = _delegate; instead
EDIT:
Works well for me
myClass.h
#import <Foundation/Foundation.h>
#interface myClass : UIImageView <UIGestureRecognizerDelegate> {
id referenceOne;
id referenceTwo;
}
#property (nonatomic,retain) id referenceOne;
#property (nonatomic,retain) id referenceTwo;
#end
myClass.m
#import "myClass.h"
#implementation myClass
#synthesize referenceOne, referenceTwo;
#end
MyBasedClass.h
#import <Foundation/Foundation.h>
#import "myClass.h"
#protocol MyBasedClassDelegate <NSObject>
#optional
- (void) doStuff;
#end
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> delegate;
}
#property(nonatomic,assign) id<MyBasedClassDelegate> delegate;
#end
MyBasedClass.m
#import "MyBasedClass.h"
#implementation MyBasedClass
#synthesize delegate;
-(void) dosmth {
referenceOne = nil;
}
#end
If you change the variable name "_delegate" to "delegate", I think it might work. (Alternatively, comment out the synthesize line and it should also work).
I ran into a similar thing earlier today -- if you synthesize properties for instance variables that do not exist, the compiler doesn't complain but for some reason you can no longer see the superclass's variables.
For me it was finding http://www.iphonedevsdk.com/forum/iphone-sdk-development/53261-unable-access-superclass-member-variables-subclass-implementation.html that helped.

Syntax for creating View delegate in Objective-C

I am trying to create a delegate protocol for a custom UIView. Here is my first attempt:
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end
#interface FunView : UIView {
#private
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
This doesn't work because the FunView interface has not been declared at the time of the FunViewDelegate declaration. I have tried adding a prototype ala C++ before the #protocol:
#interface FunView;
But this just drives the compiler nuts. How am I supposed to do this?
Forward class syntax is #class Foo;, not #interface Foo;.
It would seem that you can forward declare protocols:
#protocol FunViewDelegate;
#interface FunView : UIView {
#private
id<FunViewDelegate> delegate;
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end