A quick question if I may, can anyone explain what I am missing below, I was assuming the 3rd one with the would work?
#interface ...
// These work
#property(assign) SomeClass *someDelegate;
#property(assign) id someDelegate;
// This gives warning
#property(assign) id <SomeClassDelegate> someDelegate;
.
#implementation ...
#synthesize someDelegate;
[self setSomeDelegate:[[SomeClass alloc] init]];
[someDelegate setDelegate:self];
.
warning: method '-setDelegate:' not found (return type defaults to 'id')
EDIT_001:
// SomeClass.h
#import <Foundation/Foundation.h>
#class SomeClass;
#protocol SomeClassDelegate <NSObject>
#optional
-(void)didHappen:(SomeClass *)someClass;
#required
-(void)willUse:(SomeClass *)someClass withThis:(BOOL)flag;
#end
#interface SomeClass : NSObject {
id <SomeClassDelegate> delegate;
}
#property(assign) id <SomeClassDelegate> delegate;
-(void)otherActions;
#end
cheers Gary.
Go protocols!
#protocol MyDelegateProtocol
- (NSNumber*) someFunction:(NSArray*) anArray;
#end
#interface MyClass : NSObject {
id<MyDelegateProtocol> delegate;
}
#property id<MyDelegateProtocol> delegate
#end
Then in your #implementation:
#synthesize delegate;
As far as I know, the Cocoa way :-)
Cheers
Nik
Unless it exists and you didn't show it, you don't have a setDelegate method. You have a setSomeDelegate method, though.
Related
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
I have a subclass of UIImageView and would like to pass self as a parameter to the delegate. I get a error "Expected ')' before MyImageView". I need to pass the object to the delegate so the delegate can read certain properties from the object.
#import <UIKit/UIKit.h>
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView; //line with ERROR...
#end
#interface MyImageView : UIImageView {
id <MyImageViewDelegate> delegate;
BOOL _imageMode;
}
#property (nonatomic,assign) id <MyImageViewDelegate> delegate;
#property (nonatomic,readonly) BOOL imageMode;
I think you need a forward declaration of MyImageView before your protocol declaration.
#class MyImageView;
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView;
#end
Maybe you should define the protocol after defining the MyImageView interface?? it seems that the method its not recognizing MyImageView class type, or just define the protocol in a new .h and import your current .h.
Try adding this:
#import <UIKit/UIKit.h>
#class MyImageView; <<ADD THIS LINE
#protocol MyImageViewDelegate <NSObject>
-(void) buttonPressedForView:(MyImageView *) imageView; //line with ERROR...
#end
#interface MyImageView : UIImageView {
id <MyImageViewDelegate> delegate;
BOOL _imageMode;
}
#property (nonatomic,assign) id <MyImageViewDelegate> delegate;
#property (nonatomic,readonly) BOOL imageMode;
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.
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
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