Delegate Help - Using class the delegate is for inside the delegate - iphone

How would I use the class that the delegate is for inside of the protocol methods.
Ex:
#protocol ILMIconDelegate <NSObject>
- (void)deleteIcon:(ILMIcon *)icon;
#end
#interface ILMIcon : UIView <IconPopoverViewControllerDelegate>
...
#end
This doesn't work because I can't use (ILMIcon *) inside the protocol as it's declared later in the file.
Any help?
Is there any work around, or should I just use (UIView *) instead?
Thanks
Edit: newacct gave me the answer of using #class ILMIcon; before the protocol and it works!
Thanks alot man!

You can forward-declare the class before the protocol declaration, like:
#class ILMIcon;

Related

Delegate AVAudioPlayer for audioPlayerEndInterruption method

My ViewController name is Mainviewcontroller in which i m doing all the actions of AVAudioplayer.
If in h file i do
#class MainViewController;
#protocol MainViewControllerDelegate
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
#end
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
I am confused in the above statement i m supposed to do AVAudioPlayerDelegate or MainViewControllerDelegate
If i do #protocolAVAudioPlayerDelegate then i get warning in yellow stating that duplicate protocol definition of AVAudioPlayer is ignored
when i have not defined #protocolAVAudioPlayer anywhere else.
Another confusion i have is that i m supposed to declare these methods in h file first only then implement in m file
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)audioPlayer;
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)audioPlayer;
Please help.
I think you are mis-using the #protocol construct here. You can go ahead and drop the entire #protocol block and just implement your class. You simply need to indcate your class is a delegate for an instance of AVAudioPlayer like this:
#interface MainViewController : UIViewController <UIActionSheetDelegate, InfoDelegate, AVAudioPlayerDelegate>
Then when you instantiate your AVAudioPLayer object, be sure to assign the MainViewController as the delegate:
audioPlayInstance.delegate = self;
Lastly, implement the two methods you want to be called on your AVAudioPlayer instance (I called it audioPlayInstance above). You only need to put these in your .m file, NOT you header. The header declaration for these methods is handled by the AVAudioPlayer.h class file.
Good Luck!

protocol method is not being called by the delegate object

Another case of protocol method not being called - NO idea what am I doing wrong here...
Here is the code, omitting unnecessary info...
first header file: AccessNumber.h
#protocol AddItemDelegate <NSObject>
#required
- (void) processSuccessful: (BOOL)success;
#end
#interface AccessNumber : UIViewController <UITableViewDelegate, UITableViewDataSource, ABPeoplePickerNavigationControllerDelegate, UIAlertViewDelegate> {
id <AddItemDelegate> delegate;
}
#property (retain) id <AddItemDelegate> delegate;
#end
first .m file: AccessNumber.m - I am calling the protocol method from viewdidload just for testing purposes, it should basically get called from another method, but same thing for the sake of this convo (tried both of course)
#import "AccessNumber.h"
#import "History.h"
#synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
....
[[self delegate] processSuccessful:YES];
}
second file header: History.h
#interface History : UIViewController <UITableViewDelegate, UITableViewDataSource, AddItemDelegate> {
....
}
method implementation in history.m
- (void) processSuccessful: (BOOL)success {
NSLog(#"SUCCESS");
}
Appreciate any help. Thanks!
In the code i don't see something like:
theAccessNumber.delegate = self;
You must set the History instance as the delegate property of the AccessNumber instance.
Regards.
The code seems a little bit funny but you are not telling your "AccessNumber" class who is his delegate, even though you are making the "History" class implement the protocol established by yourself (otherwise you would get a warning).
You have to set the delegate for the "AccessNumber" class like this when setting it up from within "AccessNumber.m":
self.delegate = historyInstance;
or like this when setting it up from within "History.m":
accessNumberInstance.delegate = self;
There are very vew situations where you should retain a delegate, normally your delagate will outlive the object. So change your property from retain to assign. And be sure you are setting the delegate. Where are you doing it? If your object really depends on it you should be passing it in the constructor (iniWithDelagate). Try doing a NSLog before calling the delagate method just to see if it isn't nil.

How to make a forward declaration for private method?

I'm arranging my methods into groups using #pragma mark in implementation. But sometimes, the method implementation code appears below the code that calls this method, and I'm getting "Instance method not found" warnings. It happens when I'm using private methods. How to fix that?
Simplest method is to use a anonymous category. Add something like this to the top of your .m file, before your #implementation:
#interface MyClass()
- (void)myPrivateMethod;
#end
In your Class.m implementation file, you can add an interface section at the beginning and declare private functions in there:
#interface YourClassName (private)
-(void)aPrivateMethod:(NSString*)aParameter;
...
#end
#implementation YourClassName
...
#end
In this case, you would use a class extension inside of your implementation file to define these methods. In this manner, your 'public' API is still defined in your header file, and your implementation file contains the definition of your pseudo-private methods.
YourClass.m
#interface MyClass()
- (void)myPrivateMethod;
#end
#implementation MyClass
- (void)myPublicMethod
{
// This will not throw an error or warning
[self myPrivateMethod];
}
- (void)myPrivateMethod
{
// Do something
}
#end

iPad protocol problem

I have a little problem; I have this protocol defined as so below:
#protocol someProtocol <NSObject>
- (void) changedStoryForIndexPath: (NSIndexPath *) indexPath;
#end
I have it defined in a file named "ListViewController.h", logically I have to import the header in another file like: #import "ListViewController.h" then in my #interface declare the protocol in the protocol tags like <someProtocol> right? When do I do so then compile, the compiler (on Xcode 4.0 and 3.2.4) tells me that it can't find the protocol declaration. You can see the error here: http://www.freeimagehosting.net/uploads/5ff0c99bf7.png
Thank You guys!
You should have the protocol in a different header file.
SomeProtocol.h:
#protocol someProtocol <NSObject>
- (void) changedStoryForIndexPath: (NSIndexPath *) indexPath;
#end
And import it in the ListViewController class.
#import "SomeProtocol.h"

Objective C: Class Extensions and Protocol Conformation Warnings

I have a large class, which I have divided into several different class extension files for readability.
#protocol MyProtocol
#required
-(void)required;
#end
#interface MyClass : NSObject <MyProtocol>
#end
#interface MyClass (RequiredExtension)
-(void)required;
#end
Is there a better way to do this, without the compiler warning?
warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol
Use a category for each protocol implementation. I use this when I have complex viewControllers.
For example, I have a category that implements NSTextDelegate protocol.
So, MyComplexViewController+NSTextDelegate.h:
#import "MyComplexViewController.h"
#interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>
#end
and MyComplexViewController+NSTextDelegate.m:
#import "MyComplexViewController+NSTextDelegate.h"
#implementation MyComplexViewController (NSTextDelegate)
- (BOOL)textShouldBeginEditing:(NSText *)textObject{
...
}
- (BOOL)textShouldEndEditing:(NSText *)textObject{
...
}
- (void)textDidBeginEditing:(NSNotification *)notification{
...
}
- (void)textDidEndEditing:(NSNotification *)notification{
...
}
- (void)textDidChange:(NSNotification *)notification{
....
}
#end
Then I take all the headers for the main class definition and the categories and combine them into one header which I then import where I need to use the class.
#interface MyClass : NSObject
#end
#interface MyClass (RequiredExtension) <MyProtocol>
-(void)required;
#end
Adopt the protocol in the category.
You don't need to change your style of coding. To get around the warning, it only need to implement "required" method of the protocol, not "optional"
If that's only for readability, you should use categories only. A protocol is not needed in such a case.