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

标签 iphone ios variables view uiviewcontroller

我正在 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 。

也就是说,如果您遵循 View Controller 创建其 View 的通常的 Cocoa Touch 模式,那就会好得多。 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/

相关文章:

iphone - 使用 ARC 并针对 iOS 4.0 时如何替换弱引用?

iphone - 在 Debug模式下找不到此可执行文件的有效配置文件

ios - AVPlayer 视频始终无法加载

ios - 将 UIAlertAction 标题向左对齐

ASP.net 试图在页面上打印变量值

node.js - 变量将在已部署的 Node/express 应用程序中存储多长时间?每个客户都可以使用它吗?

iphone - iPhone OS版本AppStore部署

iphone - 如何停止 objective-c 中的图像移动?

android - 像 Google Play 一样的 iOS Alpha 测试

variables - WIX 的整数变量