我在iOS上開發,並以編程方式構建我的視圖。我注意到,當我嘗試訪問必須在視圖中從視圖控制器更改的變量時,它們爲空。我會後雙方的觀點和看法控制器:iOS從視圖控制器訪問視圖的變量
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
我改變了代碼,但它似乎沒有工作.....
好的解決我忘了這行 「self.rootView = rootV;」
對不起,我沒有得到它。如果我理解得很好,我想要做的是在我的視圖控制器中根據MVC和視圖控制器在我的視圖控制器中設置插口,我想更改標籤的屬性是否可以?我試圖添加一個按鈕,當我按下按鈕時,我嘗試更改標籤的文本,但它不起作用。似乎參考標籤爲null ..任何建議?! – ubiAle
我希望我的編輯有助於澄清事情,但是您還需要閱讀文檔中的[瞭解視圖管理週期](http://tinyurl.com/6cw5352)。 – Caleb
因此,根據通常的可可觸摸模式,我應該在loadview方法中構建視圖嗎?如果是這樣,將視圖放在單獨的文件中是否與我一樣錯誤,並在loadview方法中實例化視圖?這與我做過或沒做過的事有什麼不同嗎?!謝謝 – ubiAle