Amazon.com Widgets Protocol Question 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 07:32:15 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 11 - Categories and Protocols
| | | |-+  Protocol Question 2
Pages: [1]   Go Down
Print
Author Topic: Protocol Question 2  (Read 1233 times)
jonthornham
Full Member
***
Posts: 169



WWW Email




« on: August 28, 2010, 05:40:43 PM »

I saw this today and I believe the code below is a clever use of protocols and I was hoping someone could validate my thinking.

ViewController.h
The view controller declares the following and creates property's.  It conforms to ToolDelegate which is below.
Code: (Objective-C)
#import <UIKit/UIKit.h>
#import "Tool.h"

@interface Dudel4ViewController : UIViewController <ToolDelegate>
{
id <Tool> currentTool;
UIColor *strokeColor;
UIColor *fillColor;
CGFloat strokeWidth;
}

@property (nonatomic, retain) UIColor *strokeColor;
@property (nonatomic, retain) UIColor *fillColor;
@property (nonatomic, assign) CGFloat strokeWidth;

@end

There is a protocol called ToolDelegate which has the following
Code: (Objective-C)
@protocol ToolDelegate
-(void)addDrawable:(id <Drawable>)d;
-(UIView *)viewForUseWithTool:(id <Tool>)t;
-(UIColor *)strokeColor;
-(UIColor *)fillColor;
-(CGFloat)strokeWidth;
@end

There are a bunch of tools which the view controller uses.  The .h file has an id variable which conforms to the ToolDelegate protocol.
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Tool.h"


@interface PencilTool : NSObject <Tool>
{
id <ToolDelegate> delegate;
NSMutableArray *trackingTouches;
NSMutableArray *startPoints;
NSMutableArray *paths;
}

+(PencilTool *) sharedPencilTool;

@end

I think what is happening is the getter methods created with the property's are included in the ToolDelegate protocol.  Since the are included the id variable that conforms to it then has access to the getter method created in the View Controller.  Is this correct?

Thanks,

Jon




Logged

Jon Thornham
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: August 28, 2010, 09:34:50 PM »

(I'm a little confused about the use of Tool and ToolDelegate here and the code in general). Which class defines the ToolDelegate protocol?  I assume it's the Tool class?  

The declaration

Code: (Objective-C)
    id <ToolDelegate> delegate;  

tells the compiler that whatever object is stored in the delegate variable will conform to the ToolDelegate protocol.  This is used in many places in iPhone programming.  The delegate is set to the object that will implement the protocol methods (and is typically set to self).   Then, when the system wants to use one of those methods, it will send the message to the delegate, knowing that the delegate object's class has implemented that method.   If any of those methods were declared optional when the protocol was defined, the system would first test to see if the object implements the method (respondsToSelector: can do that) before sending the message to the object.

Overall, I don't know if there's anything "clever" going on here.  Seems like the Dude14ViewController has  declared that it conforms to the ToolDelegate protocol, which means it must implement the 5 methods defined for that protocol.

Cheers,

Steve
« Last Edit: August 29, 2010, 06:08:14 AM by skochan » Logged
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #2 on: August 29, 2010, 02:30:05 PM »

I tried to simplify it but obviously I confused things.  Here is the full file.  It ties into my question called protocol trouble.


Code: (Objective-C)
#import <UIKit/UIKit.h>

@protocol ToolDelegate;
@protocol Drawable;

@protocol Tool <NSObject>

@property (nonatomic, assign) id <ToolDelegate> delegate;
-(void) activate;
-(void) deactivate;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

-(void) drawTemporary;
@end

@protocol ToolDelegate
-(void)addDrawable:(id <Drawable>)d;
-(UIView *)viewForUseWithTool:(id <Tool>)t;
-(UIColor *)strokeColor;
-(UIColor *)fillColor;
-(CGFloat)strokeWidth;
@end

Take care,

Jon
Logged

Jon Thornham
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #3 on: August 30, 2010, 04:27:41 AM »

Nothing changes from my previous reply.  I see now this example contains a @property directive inside the protocol definition.  So is your question then about the use of the @property directive?  It just says to conform to the Tool protocol, you will need to have a delegate property in your class, with a getter and setter (implying either the implicit or explicit declaration of a corresponding delegate instance variable; the latest XCode release now fully supports implicit instance variable declarations with just the use of the @property directive--meaning in this case that you can just go ahead and @synthesize delegate; in the implementation section of the class that conforms to this protocol.  Or you can still go ahead and explicitly program the setters/getters).

Hope this answers your question.

Cheers,

Steve
« Last Edit: August 30, 2010, 04:29:40 AM by skochan » Logged
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #4 on: August 30, 2010, 08:21:37 PM »

I guess I am struggling with the way the file is set up.  Why are these lines listed first without an @end.  Plus ToolDelegate is defined later below.

Code: (Objective-C)
@protocol ToolDelegate;  
@protocol Drawable; 

If my understanding of protocols is correct we need to have the following form

Code: (Objective-C)
@protocol ProtocolName;

-(void) method1;

@end

All I see for the first two lines is @protocol ToolDelegate; and @protocol Drawable;. 

The rest of the code follows the pattern for Protocols that I am used to seeing (@protocol, -(void) method, @end)

Code: (Objective-C)
@protocol Tool <NSObject>  
 
@property (nonatomic, assign) id <ToolDelegate> delegate; 
-(void) activate; 
-(void) deactivate; 
 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;   
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;   
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;   
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
 
-(void) drawTemporary; 
@end 
 
@protocol ToolDelegate 
-(void)addDrawable:(id <Drawable>)d;   
-(UIView *)viewForUseWithTool:(id <Tool>)t;   
-(UIColor *)strokeColor;   
-(UIColor *)fillColor; 
-(CGFloat)strokeWidth; 
@end 

I know I am jumping around a bit but I am clearly confused.  I apologize.

Thanks,

Jon
Logged

Jon Thornham
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #5 on: August 31, 2010, 12:58:22 PM »

I guess I am struggling with the way the file is set up.  Why are these lines listed first without an @end.  Plus ToolDelegate is defined later below.

Code: (Objective-C)
@protocol ToolDelegate;  
@protocol Drawable;  
That just tells the compiler that ToolDelegate and Drawable are the names of two protocols.  It's not defining them here.  It's similar to the way the @class directive is used.    That allows you to write a line like

Code: (Objective-C)
-(void)addDrawable:(id <Drawable>)d;     

without having to define the Drawable protocol.  Here the compiler just knows that Drawable is the name of a protocol.  It doesn't need to know anything further about that protocol at this point.

Cheers,

Steve

« Last Edit: August 31, 2010, 01:00:14 PM by skochan » Logged
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #6 on: August 31, 2010, 08:50:29 PM »

Thank you, thank you , thank you.  That makes sense.  I appreciate the help.

Take care,

Jon
Logged

Jon Thornham
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.