gpt4 book ai didi

iphone - iOS 从 View Controller 访问 View 的变量

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:49 24 4
gpt4 key购买 nike

我在 iOS 上进行开发,并以编程方式构建我的 View 。我注意到,当我尝试从 View Controller 访问必须在我的 View 中更改的变量时,它们为空。我将发布 View 及其 View Controller :

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);
self.rootView.label.text=@"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);
}

@结束

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;”

最佳答案

您的 View 直到它的 -initRootView 方法返回后才知道它的 Controller 是什么,但您正试图从该方法中使用该 Controller 。

也就是说,如果您按照通常的 Cocoa Touch 模式创建 View Controller ,情况会好得多。 View Controller 应该延迟创建它们的 View ,也就是说它们推迟 View 创建和初始化,直到调用 -loadView 方法。您可以覆盖 -loadView 来创建 View ,也可以覆盖 -viewDidLoad 来执行创建 View 后需要完成的任何设置工作。

此外,通常不建议 View 了解其 Controller 。 Controller 应该告诉 View 做什么,而不是相反。如果您需要 View 向 Controller 发送一些信息,您通常将 Controller 作为 View 的委托(delegate)提供给 View 。但是如果你只需要 View Controller 能够找到一些 subview ,比如你的标签,那么在容器 View 中为此提供一些访问器可能是个好主意(这样 View Controller 就可以像 self.view.label.text = @"some text";。另一种选择是将 subview 的标记属性设置为某个唯一值,并让 Controller 使用它来查找 subview 。

关于iphone - iOS 从 View Controller 访问 View 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com