Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 11, 2006, 04:57 PM   #1
netytan
macrumors 6502
 
Join Date: May 2004
Saving text in document-based application.

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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?
netytan is offline   0 Reply With Quote
Old Mar 12, 2006, 03:12 AM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 12, 2006, 12:56 PM   #3
netytan
Thread Starter
macrumors 6502
 
Join Date: May 2004
Quote:
Originally Posted by HiRez
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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?
netytan is offline   0 Reply With Quote
Old Mar 12, 2006, 03:44 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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
File Type: zip BasicTextEditor.zip (30.1 KB, 85 views)
robbieduncan is offline   0 Reply With Quote
Old Mar 12, 2006, 03:52 PM   #5
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by netytan
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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 12, 2006, 05:55 PM   #6
netytan
Thread Starter
macrumors 6502
 
Join Date: May 2004
Quote:
Originally Posted by robbieduncan
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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?
netytan is offline   0 Reply With Quote
Old Mar 12, 2006, 06:47 PM   #7
netytan
Thread Starter
macrumors 6502
 
Join Date: May 2004
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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?
netytan is offline   0 Reply With Quote
Old Mar 12, 2006, 09:29 PM   #8
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by netytan
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]?
mrichmon is offline   0 Reply With Quote
Old Mar 12, 2006, 10:00 PM   #9
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by netytan
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)?
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 13, 2006, 09:02 AM   #10
netytan
Thread Starter
macrumors 6502
 
Join Date: May 2004
Quote:
Originally Posted by HiRez
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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?
netytan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
basic core data saving in a document-based app Yashman! Mac Programming 0 Apr 10, 2014 07:58 AM
Document based application, use folders (of any type) as documents printz Mac Programming 0 Oct 30, 2013 01:02 PM
Any way to close pages document without saving? jbachandouris Mac Applications and Mac App Store 1 Dec 13, 2012 12:21 PM
View based vs Window based application Vishwas Gagrani iPhone/iPad Programming 1 Oct 14, 2012 05:25 PM
Threading of Core Data in a document based application bitdoctor Mac Programming 0 Sep 25, 2012 01:30 AM

Forum Jump

All times are GMT -5. The time now is 10:56 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC