Re: Documents & Controllers
Re: Documents & Controllers
- Subject: Re: Documents & Controllers
- From: Seth Willits <email@hidden>
- Date: Sun, 10 Aug 2003 14:23:51 -0700
Thinking this over, I don't think there's a better way to ask than
explain everything. So here it goes...
To learn how to use the multi-document architecture in Cocoa I decided
to create a web browser. If you're wondering, I chose a web browser
because a) it's cool that we have WebKit b) some people don't like
Safari, Omniweb $$, and Chimera and the rest don't use WebCore, so who
knows.. c) I don't really need to write a completely useless text
editor and d) I'm not too concerned with saving or loading files at
this point since such is separate from learning the architecture. (and
now you're thinking, "then why did he ask about files?" -- I'll explain)
So I started my project with 2 basic classes: MyDocument : NSDocument,
and FWWindowController : NSWindowController. After setting up the
interface with a basic field and a webview, I created two outlets in
the FWWindowController (FWWC for short) class, one for each item. The
field sent an action to the window controller to load the page for the
given string, and it did so. This all worked handy dandy so I built on
this quite a bit.
At this point I have a few more classes and a lot more functionality. I
have a basic toolbar with address input in it, completely working
tabbed browsing, a drawer which displays the different tabs, and a
handful of menu items to link things I have put in the interface yet.
My classes at this point are:
MyDocument : NSDocument
FWWC : NSWindowController
DocToolbarController : NSObject
WebTabController : NSObject
WebTab : NSTabViewItem
The way everything works is, MyDocument does absolutely nothing. At
this point I don't have any data to keep track of since it's all either
taken care of by the WebView or my TabView. So my MyDocument header
file looks like this:
#import <Cocoa/Cocoa.h>
#import "FWWindowController.h"
@interface MyDocument : NSDocument {
NSWindowController * windowController;
}
- (NSWindowController *)windowController;
@end
There's nothing in it except for the window controller instance. My
window controller class, FWWC, is the one that contains all of the
outlets which connect the code to the interface. This includes outlets
for the two different controllers, WebTabController and
DocToolbarController, the NSTextField for address input, and the outlet
for the window's drawer.
So right now, what I have implemented works great, and the next thing
on my list is loading files using the Open menu item from the local
drive.
Knowing that MyDocument houses -loadDataRepresentation:ofType:, I
popped over there to begin coding before I realized I had no idea what
to write because of two problems which ultimately led to me to thinking
I may have gone about this entirely wrong.
The first problem: Instead of loading data from a file and giving it to
the webview to display, I instead need to get an absolute url
(file:///users/seth/Desktop/myHTMLFile.html) and pass *that* to the
webview. I couldn't figure this out because I didn't see any way to get
the file url from the NSDocument, only the file's name. So problem #1
is, how do I get the file URL?
The second and more disastorous problem is, how do I give that URL to
the WebView? Currently, the address field sends an action directly to
the window controller via the File Owner in the nib file. The action it
calls is: - (IBAction)loadURL:(NSString *)address; (I know how to
obtain the absolute path string from a NSURL so lets just assume that
somehow in someway I end up with a local url for the file I want to
open.) So the question is, how do I call that action (pretty simple to
do), but also is it this the right way to do it? Since I have no data
that I'm manipulating (I'm just pushing thing around and the other
objects retain them for me) is it correct to have all of the code in
the FWWindowController class? Or should I have created all of my
outlets in the MyDocument class? The latter doesn't seem correct.
To tell you the truth, I'm running out of reasons why I asked what I
did. I'm basically having doubts about whether I've set up my classes
correctly. And I guess one way to know is, if I *were* manipulating
data, would I have all the code exactly where it is now, but add
properties to MyDocument to contain that data as well as actions for
the FWWindowController to retrieve and set that data?
Basically, I don't know if I'm doing this correctly because I haven't
read anything that said I am or seen any code that does something
similar. It looks as if I could override readFromFile:ofType: and
writeToFile:ofType: to get my file URL and save data that I retrieve
from the WebView to a file, but I'm not entirely sure that this is what
I should be doing.
Hopefully you can now see why I'm asking what I'm asking. It may be
unnecessary or the wrong things to ask :) but hopefully you understand
my newbie perspective.
By the way, Cocoa is very very cool. :D
Oh one last question. How am I supposed to tell if a method is called
by Cocoa and available for me to override or simply there for me to
call? Or do all available calls get called by Cocoa for some reason and
thus I could override every single one? More specifically, I didn't
know that readFromFile and writeToFile were used in the NSDocument
class to write to a file. I thought they were there for me to use in
addition to loadDataRepresentation and dataRepresentationOfType. In
addition how? I don't know...
Anyway thanks for the help,
Seth Willits
------------------------------------------------------------------------
---
President and Head Developer of Freak Software -
http://www.freaksw.com
Q&A Columnist for REALbasic Developer Magazine -
http://www.rbdeveloper.com
Webmaster for REALbasic Game Central -
http://www.freaksw.com/rbgames
"Few are those who see with their own eyes and feel with their own
hearts."
-- Albert Einstein
------------------------------------------------------------------------
---
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.