I have a strange problem that I’ve never encountered before,
I have data in my viewController that I want to display in a UIView.
It’s an iPad App which involve a SplitView Controller, when I click on an element within the table view (masterView) it execute a function in my detailViewController (via a protocol).
A function is executed which launch a UIView and send data to it:
myController:
- (void)SelectionChanged:(DocumentInfo*)document_info withDocu:(Document *)document{
DocumentView *viewDoc=[[DocumentView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
viewDoc.doc=document;
viewDoc.doc_info=document_info;
[viewDoc setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:viewDoc];
}
DocumentView.h
#import <UIKit/UIKit.h>
#import "Document.h"
#import "DocumentInfo.h"
@class Document;
@class DocumentInfo;
@interface DocumentView : UIView
@property(strong,nonatomic) Document *doc;
@property(strong,nonatomic) DocumentInfo *doc_info;
@end
DocumentView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UILabel *titreDoc=[[UILabel alloc] initWithFrame:CGRectMake(20, 32, 339, 21)];
titreDoc.textColor = [self makeColorRGB_RED:66 GREEN:101 BLUE:149];
titreDoc.font = [UIFont fontWithName:@"System" size:(24.0)];
[self addSubview:titreDoc];
NSLog(@"%@ - %@",doc,doc_info);
titreDoc.text=@"Nouveau Document";
}
return self;
}
My view is well displayed (I mean the label appear) but impossible to get the data which would have been passed to it… (the NSLog print (null) (null) )
Anybody know the reason why?
The problem seems pretty straight forward. You initialize your view (which means that you run
- (id)initWithFrame:(CGRect)frame) and after that you’re setting the data, so it’s normal that you seenullvalues into yourinitmethod because the ivars have not being set yet. What you could do is to modify your init method in order to construct your view taking into account these ivars. Something like this perhaps:ps. If you choose to make your custom
initmethod do not forget to call the designated initializer (-initWithFrame:) before any customization.