在我正在處理的應用中,我有一個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