实例化后设置 ViewController 的属性 [英] Setting a ViewController's properties after instantiation

查看:29
本文介绍了实例化后设置 ViewController 的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 viewController 的实例,然后尝试在它的属性上设置文本,一个 UILabel.

I'm creating an instance of a viewController, and then trying to set the text on of it's properties, a UILabel.

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];
        NSString *newText = [astrology getSignWithMonth:month   withDay:day];
        boyViewController.sign.text = newText;
        NSLog(@" the boyviewcontroller.sign.text is now set to: %@", boyViewController.sign.text);
        [newText release];

我试过了,但没有用...

I tried this, but it didn't work...

所以我尝试了以下方法:

So I tried the following:

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];
    UILabel *newUILabel = [[UILabel alloc] init];
    newUILabel.text = [astrology getSignWithMonth:month withDay:day];
    boyViewController.sign = newUILabel;
    NSLog(@" the boyviewcontroller.sign.text is now set to: %@", newUILabel.text);
    [newUILabel release];

但无济于事..

我不知道为什么我不能在 boyViewController 中设置 UILabel "sign" 的 text 属性..

I'm not sure why I can't set the text property of the UILabel "sign" in boyViewController..

推荐答案

这里的问题是初始化程序实际上并没有将 nib 文件加载到内存中.相反,加载笔尖会延迟,直到您的应用程序请求视图控制器的 view 属性.因此,当您访问控制器时,控制器的 sign 属性为 null.

The problem here is that the initializer does not actually load the nib file into memory. Instead, loading the nib is delayed until your application requests the view controller's view property. As such, your controller's sign property is null when you access it.

手动请求控制器的 view 属性将使您的示例工作...

Manually requesting the controller's view property would make your example work...

BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil];

[boyViewController view]; // !!!: Calling [... view] here forces the nib to load.

NSString *newText = [astrology getSignWithMonth:month   withDay:day];
boyViewController.sign.text = newText;
// and so on...

然而,我猜你真正想做的是在让它自由做自己的事情之前创建和配置你的视图控制器.(例如,也许以模态方式显示它.)手动调用 [... view] 不是一个长期的解决方案.

However, I'd guess that what you're really trying to do is create and configure your view controller before setting it free to do it's own thing. (Perhaps to display it modally, say.) Calling [... view] manually is not going to be a long-term solution.

更好的是在你的视图控制器上为标签文本设置一个单独的属性,然后实现 viewDidLoad 将它分配给标签:

Better is to set a separate property on your view controller for the label text and then implement viewDidLoad to assign it to the label:

@interface BoyViewController : UIViewController {
    IBOutlet UILabel *label;
    NSString *labelText;
}
@property(nonatomic, copy)NSString *labelText;
@end

@implementation
@synthesize labelText;

- (void)viewDidLoad
{
    [label setText:[self labelText]];
}

// and so on...

@end

这有一个额外的好处,即您的标签文本会被重置,以防在低内存事件期间清除视图.

This has the added benefit of your label text being reset in case the view is purged during a low memory event.

这篇关于实例化后设置 ViewController 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆