PDA

View Full Version : Saving text in document-based application.




netytan
Mar 11, 2006, 04:57 PM
Hi all,

I have a document based app which I want to add open and save to, it's just a little text editor so it's nothing major.

The problem is that I'm using a custom NSWindowController class to handle my interface, an instance of which is my `Files Owner'. I can connect to an NSTextView outlet in this class but not in the NSDocument class :(.

How then do I implement the saving functions so that they write the data from my textView? Can they be implemented in the NSWindowController or is there a way to access the textView? I was thinking maybe I could implement a setter and getter for it...

Thanks in advance for any help,

Mark.



HiRez
Mar 12, 2006, 03:12 AM
I think you could just use accessors to get to it, but why is your window controller File's Owner? Shouldn't that be the NSDocument? I guess what I would do is to make an app controller object and run everything through there (or maybe your window controller could double as the app controller). To be honest, I often have a hard time deciding on the structure of my top level objects: Document, App Controller, etc., and how they should be connected. Eventually I settle on something that works but I never feel like I know quite how I should be setting everything up when I begin a project.

netytan
Mar 12, 2006, 12:56 PM
I think you could just use accessors to get to it, but why is your window controller File's Owner? Shouldn't that be the NSDocument? I guess what I would do is to make an app controller object and run everything through there (or maybe your window controller could double as the app controller). To be honest, I often have a hard time deciding on the structure of my top level objects: Document, App Controller, etc., and how they should be connected. Eventually I settle on something that works but I never feel like I know quite how I should be setting everything up when I begin a project.

The window controller is my Files Owner because thats how I read I was supposed to use it :).

I'm no Cocoa expert and the learning curve is massive, in just about any language I've used I can manage a reasonable result with minimal effort but with Cocoa and Obj-C there's just so much you have to know about specific aspects of the architecture. Getting anything done as a noob seems impossible :(.

Anyway I've managed to get access to the textView outlet though a getter; there's no need for a setter because I don't want to swap in a new textView :).

The problem I'm having now though is that I cant figure out how to change the text in the textView.

Does anyone know how to open and save a text file from a document based application? I've found plenty of examples that work with the NSArchiver and NSUnarchver classes but that's not what I want.

Any help would be welcome,

Mark.

robbieduncan
Mar 12, 2006, 03:44 PM
OK. I've taken pity on you. I've attached a very simple XCode project. It's a Cocoa text editor that can open and save plain text files. I wrote around 15 lines of code to get this to work.

This is the power of Cocoa. Almost no code for something simple.

HiRez
Mar 12, 2006, 03:52 PM
Does anyone know how to open and save a text file from a document based application?Well NSString has methods for both writing to and reading from files, such as writeToFile:atomically: and stringWithContentsOfFile:encoding:error:. Or you could use NSText's initWithRTFD:documentAttributes: and RTFDFromRange: if your text is styled.

netytan
Mar 12, 2006, 05:55 PM
OK. I've taken pity on you. I've attached a very simple XCode project. It's a Cocoa text editor that can open and save plain text files. I wrote around 15 lines of code to get this to work.

This is the power of Cocoa. Almost no code for something simple.

Thanks Robbie your example worked perfectly but after implementing the same methods and connecting everything up I still can't get get a file to open. Writing was working already but I think your code is cleaner than what I had using NSData.

I don't understand why I can't change the text view's contents. I can obviously access the outlet so it just strikes me as weird, this is what I found before and I figured that I was just doing something wrong.

Is there some extra configuration I need to do in order to allow reading and writing? Maybe in a plist somewhere?

Mark.

netytan
Mar 12, 2006, 06:47 PM
Ok I've tracked it down to my -windowControllerDidLoadNib not being called because I'm overriding -makeWindowControllers.

I've scoured the documentation and I haven't found any other methods that might do the same thing. Any ideas guys?

Mark.

mrichmon
Mar 12, 2006, 09:29 PM
Ok I've tracked it down to my -windowControllerDidLoadNib not being called because I'm overriding -makeWindowControllers.

I've scoured the documentation and I haven't found any other methods that might do the same thing. Any ideas guys?

Mark.

Shouldn't your makeWindow Controllers perform a call to [super makeWindowControllers]?

HiRez
Mar 12, 2006, 10:00 PM
Ok I've tracked it down to my -windowControllerDidLoadNib not being called because I'm overriding -makeWindowControllers.Have you called [myWindowController setDocument:myDocument]; to tell your window controller which document it's associated with (or connected this in IB)?

netytan
Mar 13, 2006, 09:02 AM
Have you called [myWindowController setDocument:myDocument]; to tell your window controller which document it's associated with (or connected this in IB)?

Thanks for the reply man,

Most certainly but I've changed the setup a little. The NSDocument subclass is now the Files Owner like you suggested :). Reading the documentation it sounds like windowControllerDidLoadNib is only called if windowNibName is overridden I've tested this and it works fine however it bypasses all of the work I've done in my custom windowController.

What I'm looking for is a way to update the NSTextView when the nib is loaded and keep my windowController.

Thanks for any advice :),

Mark.