Understanding Protocol Inheritance in Objective-C - iphone

I'll appreciate if anyone can explain the logic behind protocol inheritance. e.g. what does the following mean (UITableView.h):
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
The following class implementation doesn't work. I have a class View1 (which inherits UIView), with an associated protocol. I have another class, View2 (which inhertits View1). Now i want to inherit the the protocol as well. Can anyone please point me in the right direction.
Class 1:
#protocol View1Delegate;
#interface View1 : UIView {
id <View1Delegate> delegate;
// . . .
}
#property (nonatomic, assign) id <View1Delegate> delegate; // default nil. weak reference
#end
#protocol View1Delegate <NSObject>
- (void)View1DelegateMethod;
#end
#implementation View1
#synthesize delegate;
// . . .
#end
Class 2:
#protocol View2Delegate;
#interface View2 : View1 {
id <View2Delegate> delegate;
// . . .
}
#property (nonatomic, assign) id <View2Delegate> delegate; // default nil. weak reference
#end
#protocol View2Delegate <NSObject>
- (void)View2DelegateMethod;
#end
#implementation View2
#synthesize delegate;
// . . .
#end

Think of it more as composition rather than inheritance.
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> defines a protocol that includes all the methods of the NSObject protocol, the UIScrollViewDelegate protocol, as well as any methods defined for the UITableViewDelegate protocol. When you subclass and create a new property, you're overriding the type of the superclasses property. To make this work how I think you want, you should declare View2Delegate as #protocol View2Delegate <NSObject, View1Delegate>.

It's exactly the same as the inheritance of interfaces in Java (interface UITableViewDelegate extends NSObject, UIScrollViewDelegate), C# (interface UITableViewDelegate : NSObject, UIScrollViewDelegate), etc.

Related

Cocos2d ,need to include protocol in my implementation file

So basically I have a protocol inside my interface that I need to include in my implementation because I am getting an incomplete error and therefore can't continue.
. h file
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
{
...
}
.m file
#implementation waveLayer1
GameKitHelper.h file
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#protocol GameKitHelperProtocol
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived: (NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
#end
#interface GameKitHelper : NSObject {
id<GameKitHelperProtocol> delegate; bool isGameCenterAvailable; NSError* lastError;
}
#property (nonatomic, retain) id<GameKitHelperProtocol> delegate;
#property (nonatomic, readonly) bool isGameCenterAvailable; #property (nonatomic, readonly) NSError* lastError;
+(GameKitHelper*) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
-(void) getLocalPlayerFriends;
-(void) getPlayerInfo:(NSArray*)players;
#end
The error is "Method in protocol not implemented" I have more files I can show ,but to save room I decided to see if you can help me fix this with just these codes
#interface waveLayer1 : CCLayer <GameKitHelperProtocol>
This says that "wavelayer1" implements the protocol "GameKitHelperProtocol".
Method in protocol not implemented
says that a method declared in a protocol has not been implemented. Chances are that you forgot to implement one of the "GameKitHelperProtocol" methods, which makes your class NOT implement that protocol, which violates the declaration you made, which causes the compiler to output an error.
Implement these 3 methods in your waveLayer1 class..
-(void) onLocalPlayerAuthenticationChanged;
-(void) onFriendListReceived:(NSArray*)friends;
-(void) onPlayerInfoReceived:(NSArray*)players;
When you declare that a class adopts a protocol, you must write an implementation for all required methods that are defined in that protocol. So in this case, you need to add method implementations that are defined in GameKitHelperProtocol.

Trying to Implement Delegate Inheritance

I have a class called ToolbarView which is a subclass of UIView and basically creates a UIView that has a disappearing / reappearing UIToolbar on top. I also have a subclass of ToolbarView called DraggableToolbarView enables the user to drag the view around the screen.
I need to create a delegate for ToolbarView so it can notify another object / class of when the toolbar reappears and disappears. I also need to create a delegate for DraggableToolbarView so I can notify another object / class when the view is dragged. DraggableToolbarViews delegate will also need to notify another object / class of when the toolbar reappears and disappears.
So I decided to implement ToolbarViewDelegate, and have DraggableToolbarViewDelegate inherit from it and have its own method like following:
ToolbarView.h
#import <UIKit/UIKit.h>
#protocol ToolbarViewDelegate;
#interface ToolbarView : UIView <UIGestureRecognizerDelegate>
{
id <ToolbarViewDelegate> _toolbarViewDelegate;
}
#property(nonatomic, assign) id <ToolbarViewDelegate> toolbarViewDelegate;
#end
ToolbarView.m
#import "ToolbarView.h"
#import "ToolbarViewDelegate.h"
...
- (void) showBars
{
...
if (self.toolbarViewDelegate)
{
[self.toolbarViewDelegate toolbarViewWillShowToolbar:self];
}
...
}
- (void) hideBars
{
...
if (self.toolbarViewDelegate)
{
[self.toolbarViewDelegate toolbarViewWillHideToolbar:self];
}
...
}
ToolbarViewDelegate.h
#class ToolbarView;
#protocol ToolbarViewDelegate
#required
- (void) toolBarViewWillShowToolbar:(ToolbarView *)toolbarView;
- (void) toolBarViewWillHideToolbar:(ToolbarView *)toolbarView;
#end
DraggableToolbarView.h
#import "ToolbarView.h"
#protocol DraggableToolbarViewDelegate;
#interface DraggableToolbarView : ToolbarView
{
id <DraggableToolbarViewDelegate> _draggableToolbarViewDelegate;
}
#property(nonatomic, assign) id <DraggableToolbarViewDelegate> draggableToolbarViewDelegate;
#end
DraggableToolbarView.m
#import "DraggableToolbarView.h"
#import "DraggableToolbarViewDelegate.h"
...
- (void)drag:(UIPanGestureRecognizer *)sender
{
...
if (self.draggableToolbarViewDelegate)
{
[self.draggableToolbarViewDelegate draggableToolbarViewWillDrag:self];
}
...
}
...
DraggableToolbarViewDelegate.h
#import "ToolbarViewDelegate.h"
#class DraggableToolbarView;
#protocol DraggableToolbarViewDelegate <ToolbarViewDelegate>
#required
- (void) draggableToolbarViewWillDrag:(DraggableToolbarView *)draggableToolbarView;
#end
SomeViewController.h
#import <UIKit/UIKit.h>
#import "ToolbarViewDelegate.h"
#import "DraggableToolbarViewDelegate.h"
#interface SomeViewController : UIViewController <ToolbarViewDelegate, DraggableToolbarViewDelegate>
{
}
#end
SomeViewController.m
#import "DraggableToolbarView.h"
...
- (void) toolbarViewWillShowToolbar:(ToolbarView*)toolbarView
{
//NSLog(#"Toolbar Showed");
}
- (void) toolbarViewWillHideToolbar:(ToolbarView*)toolbarView
{
//NSLog(#"Toolbar Hidden");
}
- (void) draggableToolbarViewWillDrag:(DraggableToolbarView*)draggableToolbarView
{
//NSLog(#"Dragged");
}
...
[draggableToolbarView setDraggableToolbarViewDelegate:self];
...
When I do this only the DraggableToolbarDelegate methods are responding. However when I also do [drabbleToolbarView setToolbarViewDelegate:self] it works. I've tried doing each delegate separately without inheritence and it works fine so I believe the problem isn't in any other part of the code.
Anyone might know why? I figured by making the protocols inherit, I wouldn't also have to set the ToolbarViewDelegate for a DraggableToolbar object.
UPDATE: Added a lot more code
In your code, any given DraggableToolbarView instance has two properties to connect to delegates, one called toolbarViewDelegate which it inherits from its superclass, and one called draggableToolbarViewDelegate which is defined in DraggableToolbarView itself. You've got to set both of those if you want the controller to get all the delegate messages.
What you're trying to do is possible, however. You need to use the same property name in both your view classes, so that there is only one delegate connection for any instance.
First, change the name of the delegate in the superclass. (Note that you don't need, and indeed shouldn't bother, to declare an ivar for the property -- it's created by #synthesize.)
#interface ToolbarView : UIView <UIGestureRecognizerDelegate>
#property (nonatomic, assign) id <ToolbarViewDelegate> delegate;
#end
You will use the same property name in the subclass.
#interface DraggableToolbarView : ToolbarView
#property (nonatomic, assign) id <DraggableToolbarViewDelegate> delegate;
#end
This is allowed as long as the name of the backing ivar in the subclass is different than that of the superclass, e.g.,
// In superclass
#synthesize delegate;
// In subclass
#synthesize delegate = delegate_;
Now change all the delegate messages in the two view classes to use this one property:
- (void)showBars
{
if (self.delegate)
{
[self.delegate ...
- (void)drag:(UIPanGestureRecognizer *)sender
{
//...
if (self.delegate)
{
[self.delegate ...
Now you can send setDelegate: to a DraggableToolbarView and it will use the same delegate for the dragging methods and the show/hide methods.
Finally, a terminology/explanatory note. In response to your previous question, Caleb used the correct term for "stacked" protocols, and Richard did not. Protocols don't inherit from each other, but one protocol can adopt the other. The relationship is similar, but distinct. When an object conforms to a protocol, it promises to implement the methods declared in that protocol. No implementation comes along with the protocol. The same is true of one protocol adopting the other -- the methods are just declared to exist in both. When you write:
#protocol DraggableToolbarViewDelegate <ToolbarViewDelegate>
you are saying that any object which promises to implement DraggableToolbarViewDelegate's methods will also implement the methods from ToolbarViewDelegate. That's all that it means. Again, no implementation comes along with that promise.
In this case, that means that a DraggableToolbarView can expect its delegate to implement the methods in ToolbarViewDelegate.
You have not given the entire code, but from whatever is out here,
Make sure that
Your ToolBarView and its subclasses have an id <ToolBarViewDelegate> delegate as a property.
Your DraggableToolbarViewDelegate extends NSObject protocol.
and your other ViewController object conforms to delegate protocol and not the toolbarview.
Once your controller gives implementation of delegates methods and conforms to the protocol, set the delegate of view's object to self and then use delegate property set in the view to call these protocol methods.

How to use delegate methods to pass objects between two view controllers?

I'm using the Utility Application project to Xcode 4.2, then I am adding two text fields for each view: main view and the flipside view.
Now I wish to assign the value of the flipside text field to mainview text field.
MainViewController.h
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UITextField *nameField;
}
#property(nonatomic, retain) IBOutlet UITextField *nameField;
- (IBAction)showInfo:(id)sender;
#end
FlipsideViewController.h
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController {
UITextField *changeText;
}
#property (nonatomic, retain) IBOutlet UITextField *changeText;
#property (assign, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
FlipsideViewController.m
#import "FlipsideViewController.h"
#implementation FlipsideViewController
#synthesize delegate = _delegate;
#synthesize changeText;
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
When the done action starts, i want the changetext value be assigned to nameField text.
How do I do this?
In MainViewController.m, implement the flipSideViewControllerDelegate method as:
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
self.nameField.text=controller.changeText.text;
}
So when the done: method is called, this delegate method is also called with your flipSideViewController object as the argument, through which changeText can be accessed.
EDIT to answer question in comment:
In your protocol FlipSideViewControllerDelegate, add this method:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
Then it is similar to the other delegate method implementation in MainViewController.m
, which is how the protocol works really. If your MainViewController conforms to the protocol, it can implement methods of that protocol. By default all the methods declared in the protocol are optional, but you have the option to specify if the method is optional or required by using
#optional
//list of methods
#required
//list of methods
Bear in mind that if your a method is declared as required in the protocol, any class conforming to that protocol must implement it. Anyway, in your MainViewController.m:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
{
int anInt=indexPath.row;
self.nameField.text=[NSString stringWithFormat:#"%d",anInt];
}

Properly declare delegation in Objective C (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

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