1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Saving text in document-based application.

Discussion in 'Mac Programming' started by netytan, Mar 11, 2006.

  1. macrumors 6502

    netytan

    #1
    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.
     
  2. macrumors 601

    HiRez

    #2
    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.
     
  3. macrumors 6502

    netytan

    #3
    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.
     
  4. Moderator

    robbieduncan

    Staff Member

    #4
    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.
     

    Attached Files:

  5. macrumors 601

    HiRez

    #5
    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.
     
  6. macrumors 6502

    netytan

    #6
    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.
     
  7. macrumors 6502

    netytan

    #7
    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.
     
  8. macrumors 6502a

    #8
    Shouldn't your makeWindow Controllers perform a call to [super makeWindowControllers]?
     
  9. macrumors 601

    HiRez

    #9
    Have you called [myWindowController setDocument:myDocument]; to tell your window controller which document it's associated with (or connected this in IB)?
     
  10. macrumors 6502

    netytan

    #10
    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.
     

Share This Page