Objective c 设置ViewController';实例化后的属性
我正在创建viewController的一个实例,然后尝试在它的一个属性(UILabel)上设置文本Objective c 设置ViewController';实例化后的属性,objective-c,properties,Objective C,Properties,我正在创建viewController的一个实例,然后尝试在它的一个属性(UILabel)上设置文本 BoyController *boyViewController = [[BoyController alloc] initWithNibName:@"BoyView" bundle:nil]; NSString *newText = [astrology getSignWithMonth:month withDay:day]; boyViewControll
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];
我试过了,但没用
因此,我尝试了以下方法:
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”的文本属性。您是否在Interface Builder中绑定了插座 似乎您需要将第一个示例的符号出口绑定到Interface Builder中,以便实际将该文本设置为您想要的任何内容 在Interface Builder中将插座绑定到实际的UI组件后,您应该能够执行以下操作:
NSString *newText = [astrology getSignWithMonth:month withDay:day];
[[boyViewController sign] setText:newText];
是您需要了解的关于绑定的内容
您的第二个示例对我来说毫无意义。您是否在Interface Builder上绑定了插座 似乎您需要将第一个示例的符号出口绑定到Interface Builder中,以便实际将该文本设置为您想要的任何内容 在Interface Builder中将插座绑定到实际的UI组件后,您应该能够执行以下操作:
NSString *newText = [astrology getSignWithMonth:month withDay:day];
[[boyViewController sign] setText:newText];
是您需要了解的关于绑定的内容
您的第二个示例对我来说毫无意义。这里的问题是初始值设定项实际上没有将nib文件加载到内存中。相反,加载nib会延迟,直到应用程序请求视图控制器的
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...
然而,我猜您真正想做的是在设置视图控制器之前创建并配置它,然后再让它自由地完成自己的事情。(比如说,可能是以模式显示。)手动调用[…视图]
并不是一个长期的解决方案
更好的方法是在视图控制器上为标签文本设置单独的属性,然后实现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
如果在内存不足的事件中清除视图,则标签文本会被重置。这里的问题是初始值设定项实际上没有将nib文件加载到内存中。相反,加载nib会延迟,直到应用程序请求视图控制器的
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...
然而,我猜您真正想做的是在设置视图控制器之前创建并配置它,然后再让它自由地完成自己的事情。(比如说,可能是以模式显示。)手动调用[…视图]
并不是一个长期的解决方案
更好的方法是在视图控制器上为标签文本设置单独的属性,然后实现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
如果在内存不足事件期间清除视图,则标签文本将被重置,这具有额外的好处