I created this test case as a simple example of my problem.
- AppDelegate initializes the TestViewController and adds it to the window
- TestViewController initializes TestView and makes it look
- TestView initializes TestSubView and adds it as a subview
My goal is to provide TestSubView through delegate access to the methods and variables of TestViewController. In this example, TestSubView accesses methods through touchsBegan or touchhesMoved.
Please, help. Thank.
EDITOR: still not working, although no more errors. What I did: I moved the protocol definition to a separate file and imported it, saved it for assignment, and deleted the @protocol TestDelegate declaration at the beginning of the header.
I think my problem is that I do not assign a delegate to TestViewController.m, if this is a problem, how would I do it?
TestViewController.h
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <TestDelegate> {
int number;
}
-(void)assignNumber:(int)value;
-(void)displayNumber;
@property int number;
@end
Move to protocol.h and import where required
@protocol TestDelegate
-(void)assignNumber:(int)value;
-(void)displayNumber;
@end
TestViewController.m
#import "TestViewController.h"
#import "TestView.h"
@implementation TestViewController
@synthesize number;
- (void)loadView {
TestView *myView = [[TestView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.view = myView;
[myView release];
}
-(void)assignNumber:(int)value {
NSLog(@"Number Assigned");
number = value;
}
-(void)displayNumber {
NSLog(@"%i",number);
}
TestSubView.h
#import <UIKit/UIKit.h>
#import "TestViewController.h"
@interface TestSubView : UIView {
id<TestDelegate> delegate;
}
@property (nonatomic, retain) id<TestDelegate> delegate;
@end
TestSubView.m
#import "TestSubView.h"
#import "TestViewController.h"
@implementation TestSubView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setBackgroundColor:[UIColor redColor]];
[self setUserInteractionEnabled:YES];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate assignNumber:5];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate displayNumber];
}