I trying to learn Xcode, Cocoa, Objective C. Today I thought I was going to make a simple app the uses the Nac file browser, reads the file and displays it in an NSTevtView.
Everything acts like it wants to work but the file never displays. If I debug it, it shows my string has the file contents but I can’t get it to display to the TextView.
Here is my code, OpenUp.h and OpenUp.m
#import <Foundation/Foundation.h>
@interface OpenUp : NSObject
{
NSString *reader;
IBOutlet NSTextField *mainField;
}
@property(readwrite,copy)NSString *reader;
- (void)awakeFromNib;
- (IBAction)openExistingDocument:(id)sender;
@end
//OpenIt.m
#import "OpenUp.h"
@implementation OpenUp
@synthesize reader;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (IBAction)openExistingDocument:(id)sender{
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel retain];
// This method displays the panel and returns immediately.
// The completion handler is called when the user selects an
// item or cancels the panel.
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
NSURL* theDoc = [[panel URLs] objectAtIndex:0];
NSLog(@"%@", theDoc);
//open the document
NSError *error;
self.reader = [[NSString alloc] initWithContentsOfURL: theDoc encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
}
// Balance the earlier retain call.
[panel release];
}];
[mainField setStringValue: self.reader];//this should display the contents of the string
}
- (void)awakeFromNib {
self.reader = @"";//initialize reader
}
@end
I’m not sure if I’m doing something wrong with memory allocation, or if a string read from a file won’t use this windows. I’ve tried both the NSScrollViewer and the NSTextField
Everything compiles and acts like it wants to work it just wont display the string.
Any help is very much appreciated.
Mike
Make sure the
IBOutletis actually connected in the XIB. Try logging its value after you set its string.