Setting the text of an NSTextField programmatically

▼魔方 西西 提交于 2019-12-12 05:01:16

问题


I have a GUI built on IB (Xcode 4).
It has a Static Text field connected to an NSTextField. After reading the information from an XML file it's supposed to change the text to whatever it is coming from the XML

the .h is as follow:

IBOutlet NSTextField * DataBaseLocation;

the .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.

yes, I checked the connections in IB. Any ideas? thanks!


回答1:


Found the answer.

I needed to initialize the NSXMLDocument with NSXMLDocumentTidyXML like:

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



回答2:


You should print out DBLocationString instead of DBLoc to make sure it's not empty or in some corrupted format that can't be passed as a string value and go from there.



来源:https://stackoverflow.com/questions/6862848/setting-the-text-of-an-nstextfield-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!