Cra*_*aig 1 properties objective-c
我正在创建一个viewController的实例,然后尝试设置它的属性文本,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];
Run Code Online (Sandbox Code Playgroud)
我尝试过这个,但它不起作用......
所以我尝试了以下方法:
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];
Run Code Online (Sandbox Code Playgroud)
但无济于事..
我不知道为什么我不能在boyViewController中设置UILabel"sign"的text属性.
这里的问题是初始化程序实际上并没有将nib文件加载到内存中.相反,加载nib会延迟,直到您的应用程序请求视图控制器的view属性.因此,sign当您访问控制器时,控制器的属性为null.
手动请求控制器的view属性将使您的示例工作...
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...
Run Code Online (Sandbox Code Playgroud)
但是,我猜你真正要做的就是创建和配置你的视图控制器,然后再自由设置它.(也许以模态方式显示它.).[... view]手动调用不会是一个长期的解决方案.
更好的方法是在视图控制器上为标签文本设置一个单独的属性,然后实现viewDidLoad将其分配给标签:
@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
Run Code Online (Sandbox Code Playgroud)
如果在低内存事件期间清除视图,则还可以重置标签文本.
| 归档时间: |
|
| 查看次数: |
1459 次 |
| 最近记录: |