Je développe sur iOS et je construis mes vues par programme. J'ai remarqué que lorsque j'essaie d'accéder à des variables qui doivent être modifiées à partir du contrôleur de vue, elles sont nulles. Je posterai à la fois la vue et son contrôleur de vue:iOS accédant à la variable vue du contrôleur de vue
RootViewController.h
#import <UIKit/UIKit.h>
@class RootView;
@interface RootViewController : UIViewController {
RootView *rootView;
}
@property (nonatomic,retain) RootView *rootView;
@end
RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
@implementation RootViewController
@synthesize rootView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[rootView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)loadView{
RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
rootV.rootViewController = self;
self.view = rootV;
[rootV release];
}
- (void)viewDidLoad{
NSLog(@"TEXT: %@",self.rootView.label.text);
[email protected]"HELLO!";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self setRootView:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
RootView.h
#import <UIKit/UIKit.h>
@class RootViewController;
@interface RootView : UIView {
RootViewController *rootViewController;
UILabel *label;
}
@property (nonatomic,assign) RootViewController *rootViewController;
@property (nonatomic,retain) UILabel *label;
@end
RootView.m
#import "RootView.h"
#import "RootViewController.h"
@implementation RootView
@synthesize rootViewController;
@synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Create the label
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
//Set the font bold
testLabel.font = [UIFont boldSystemFontOfSize:20.0];
//Set the backgroundcolor of the label to transparent
testLabel.backgroundColor = [UIColor clearColor];
//Set the text alignment of the text label to left
testLabel.textAlignment = UITextAlignmentLeft;
//Set the text color of the text label to black
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"01:30";
self.label = testLabel;
[self addSubview:label];
[testLabel release];
}
return self;
}
- (void)dealloc
{
[label release];
rootViewController = nil;
[super dealloc];
}
@end
J'ai changé le code, mais il semble ne fonctionne pas .....
Ok j'ai oublié résolu cette ligne "self.rootView = rootv;"
Désolé je n'ai pas compris. Ce que j'essaie de faire si j'ai bien compris, c'est d'avoir des prises dans mon contrôleur de vue selon le MVC et du contrôleur de vue je veux changer les attributs de l'étiquette est-ce correct? J'ai essayé d'ajouter un bouton et quand j'appuie sur le bouton j'essaie de changer le texte de l'étiquette mais ça ne marche pas. Semble la référence à l'étiquette est nulle..une suggestion ?! – ubiAle
J'espère que mon édition aidera à clarifier les choses, mais vous voudrez également lire sur [Comprendre le cycle de gestion de vue] (http://tinyurl.com/6cw5352) dans les docs. – Caleb
Donc, selon le modèle de contact de cacao habituel dois-je construire la vue dans la méthode loadview? Si oui, est-ce mal d'avoir la vue dans un fichier séparé comme je l'ai fait et d'instancier l'affichage dans la méthode loadview? Est-ce différent de ce que j'ai fait ou pas ?! Merci – ubiAle