In the app I'm working on, I have a UIViewController
sublcass and a UIView
subclass. in the storyboard
the view controller contains the UIview
. in the uiview I'm drawing something but I need it to know some values that it should be getting from the view controller. So I created a custom protocol in the view controller .h file:
@protocol SSGraphViewControllerProtocol <NSObject>
- (void)numberOfSemesters:(int)number;
@end
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
and in the UIView
class I confirmed it as having the protocol above and I implemented its method. However. when I pass a number from the view controller, UIView
doesn't receive it. Using NSLog, I figured out that UIView
isn't entering - (void)numberOfS:(int)number;
am I doing anything wrong? How can I fix it? and is there another way that I can send data from the UIViewController
class to the UIView
controller?
Here is the full code: UIViewController.h
@protocol SSGraphViewControllerProtocol <NSObject>
- (void)numberOfSemesters:(int)number;
@end
@interface SSGraphViewController : UIViewController
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
@end
UIViewController.m
@implementation SSGraphViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.delegate numberOfSemesters:2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UIView.h
@interface SSGraph : UIView <SSGraphViewControllerProtocol>
@end
UIView.m
static int numberOfS = 0;
@implementation SSGraph
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
SSGraphViewController *graph = [[SSGraphViewController alloc] init];
graph.delegate = self;
return self;
}
- (void) numberOfSemesters:(int)number{NSLog(@"YES");
numberOfSemesters= number;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
}