I’ve created a custom class with various methods in objective-c and I’ve also added delegate methods to that class that can be called from another class that imports my custom class. In particular I’m working with iOS. Here’s a small sample of both my header (.h) and implementation (.m) files:
//iCloud.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@protocol iCloudDelegate;
@class iCloud;
@interface iCloud : NSObject {
__weak id<iCloudDelegate> delegate_;
}
@property (nonatomic, weak) id <iCloudDelegate> delegate;
//Methods (abridged for StackOverflow)
+ (void)checkCloudAvailability;
@end
@class iCloud;
@protocol iCloudDelegate <NSObject>
//Methods (abridged for StackOverflow)
@optional
- (void)documentWasSaved;
@required
- (void)iCloudAvailable:(BOOL)status;
@end
And part of my implementation (.m):
#import "iCloud.h"
@implementation iCloud
@synthesize delegate;
- (void)documentWasSaved {
[[self delegate] documentWasSaved];
}
-(void)iCloudAvailable:(BOOL)status {
[[self delegate] iCloudAvailable:status];
}
+ (void)checkCloudAvailability {
NSLog(@"Checking iCloud availablity...");
NSURL *returnedURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (returnedURL){
NSLog(@"iCloud is available");
} else {
NSLog(@"iCloud not available. ☹");
}
}
I’ve looked through numerous tutorials, SO postings, sample code, and more to try to figure out how to tell the delegate WHEN to call the delegate methods, but I haven’t been able to find anything. Most postings show that the implementation should add the delegate (using the <DelegateName> syntax), however my custom class implementation is not where I want to use the delegate (it’s in another class where the delegate is NOT being created in).
As you can see in the sample code above, I’ve added the delegate methods (in the implementation) but as far as I’m aware all this will do is call the delegate methods in the other classes that use it. If this is correct, then how do I get the delegate to call those methods (say from my checkCloudAvailability class which is not one of the delegate methods)? Here’s an example of what I’m trying to do:
+ (void)checkCloudAvailability
if (returnedURL){
[[self delegate] iCloudAvailable:YES];
...
}
When I try to use the [[self delegate] methodName]; line I get the following error, and Xcode pretends like it doesn’t know what I’m talking about anymore: 
Why is this happening? Why can’t I tell the delegate when to call the delegate methods? This works fine inside of the delegate methods, but why not in the class in which it is being implemented?
Thoughts, ideas, links, tutorials, code, or help of any kind will be appreciated.
The error says that it cannot find a method
delegateis not available in the context of a class method (i.e. one denoted with a+sign). This may look confusing, becauseselfinside a class method does not correspond to an instance, but to a class; the propertydelegate, on the other hand, belongs to an instance, meaning that it is not available.To address this, you should either make
checkCloudAvailabilityan instance method, or rewrite it as follows: