简体   繁体   English

以编程方式设置NSTextField的文本

[英]Setting the text of an NSTextField programmatically

I have a GUI built on IB (Xcode 4). 我有一个基于IB(Xcode 4)的GUI。
It has a Static Text field connected to an NSTextField. 它具有连接到NSTextField的静态文本字段。 After reading the information from an XML file it's supposed to change the text to whatever it is coming from the XML 从XML文件中读取信息后,应该将文本更改为XML中的内容

the .h is as follow: .h如下:

IBOutlet NSTextField * DataBaseLocation;

the .m .m

NSMutableArray* DBLoc = [[NSMutableArray alloc] initWithCapacity:1];
NSXMLElement* root  = [doc rootElement];
NSArray* DBarray = [root nodesForXPath:@"//DataBaseLocation" error:nil];
for(NSXMLElement* xmlElement in DBarray)
    [DBLoc addObject:[xmlElement stringValue]];

NSString * DBLocationString = [DBLoc objectAtIndex:0];
[DataBaseLocation setStringValue:DBLocationString];

NSLog(@"DBLoc: %@", DBLoc);

The NSLog shows that DBLoc has the correct string, yet the Text Field is empty and never gets set. NSLog显示DBLoc具有正确的字符串,但“文本字段”为空且从未设置。

yes, I checked the connections in IB. 是的,我检查了IB中的连接。 Any ideas? 有任何想法吗? thanks! 谢谢!

Found the answer. 找到了答案。

I needed to initialize the NSXMLDocument with NSXMLDocumentTidyXML like: 我需要使用NSXMLDocumentTidyXML初始化NSXMLDocument ,例如:

NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL fileURLWithPath:input] options:NSXMLDocumentTidyXML error:NULL];

您应该打印出DBLocationString而不是DBLoc,以确保它不为空或格式错误,不能作为字符串值传递并从那里去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM