在我正在处理的应用中,我有一个UIViewController
sublcass和一个UIView
子类。在storyboard
视图控制器包含UIview
。在uiview中,我绘制了一些东西,但我需要它来了解它应该从视图控制器获得的一些值。因此,我在视图控制器创建的自定义协议h文件:自定义协议不起作用
@protocol SSGraphViewControllerProtocol <NSObject>
- (void)numberOfSemesters:(int)number;
@end
@property (weak, nonatomic) id <SSGraphViewControllerProtocol> delegate;
并在UIView
I类确认它作为具有上述的协议和我实现它的方法。然而。当我从视图控制器传递一个数字时,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
{
}
你确定该视图的故事板中的类是正确的吗? –
请张贴更多的代码,以便我们为您提供帮助。你在哪里实现了自定义协议,你是否使用view.delegate = self等... – Scar
是的,视图控制器具有自定义类作为我创建的自定义类,它的视图也有我创建的自定义类。顺便说一句,我一直在与协议和代表团合作,并且我意识到我无法通过导航将代表从父母发送给孩子。真的吗? – HusseinB