在我正在开发的应用程序中,我有一个子类UIViewController
和一个UIView
子类。在storyboard
视图控制器中包含UIview
. 在 uiview 我正在绘制一些东西,但我需要它知道它应该从视图控制器获取的一些值。所以我在视图控制器 .h 文件中创建了一个自定义协议:
@protocol SSGraphViewControllerProtocol <NSObject>
- (void)numberOfSemesters:(int)number;
@end
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
在UIView
课堂上,我确认它具有上述协议,并实现了它的方法。然而。当我从视图控制器传递一个数字时,UIView
没有收到它。使用 NSLog,我发现UIView
没有进入- (void)numberOfS:(int)number;
是我做错了什么吗?我该如何解决?还有另一种方法可以将数据从UIViewController
类发送到UIView
控制器吗?
这是完整的代码: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
{
}