开发者

Setting the text of an NSTextField programmatically

开发者 https://www.devze.com 2023-03-23 09:55 出处:网络
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 fr

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!


Found the answer.

I needed to initialize the NSXMLDocument with NSXMLDocumentTidyXML like:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消