Lefora Free Forum
login join
Loading
232 views

encapsulating code

Page 1
1–6
regular - member
54 posts

I hope that is the correct title for this thread. I am making the switch from applescript to cocoa. I am tinkering with my first application and have read a number of tutorials and have a couple of cocoa books from the library. But my impatience got the better of me and I just had to write some code to solidify my thoughts. I am at the absolute beginning of an app to geocode some addresses for my tomtom gps device. I have a basic interface made with a controller class(myController.h and .m) and all of the IB controls are connected to and from it accordingly. right now myController.m has:

- (IBAction)chooseFile:(id)sender
{
  ...generic code to open a dialog and choose a file
}

That works, woohoo! But I quickly see I think I want that to be in a separate class that does file related stuff, like read,write,save,open etc. What's the best way to go about it? Obviously I need a new class with a choosefile method in it. I think I can write the class, just not clear on how to use it in myController. after that.

If any more information is needed please let me know.

Thank you very much,
Todd

superstar - member
233 posts

Hi Todd

I'm not sure if I know the scope of your question.  If you're wonder how to get a 'File Open' Dialog to appear, you can do that with NSOpenPanel - see page 250 of Aaron's book (3rd Edition).  You're now discovered how to add something to a menu which will then invoke your code and you can open/save and so on with NSOpenPanel.

If you're using a Document template, the folks at Apple have already wired everything for you and this supports the Model-View-Controller (see page 123 of the book).

However, if you're wondering how to separate your 'core' code from the GUI stuff, then don't forget that Obj/C is C !  (and Obj/C++ is C++).  So if you've already got 'core' code that can do the work then the good news is that you can compile and link it in.

Perhaps you could explain a little more about your what your thinking about and that will give me more focus on answers.  I strongly recommend that you work your way through Aaron's book before you start trying to write your own applications.  My reason to recommend this is because Cocoa is a rich and flexible environment.  When you understand more about the toolkit, this will have a huge influence on how you approach things.

__________________
regular - member
54 posts

Thank you for responding Robin,

I want to appologize ahead of time for the fuzzy ideas, they make perfect sense in my head and sometimes have problems describing it to others.

 I can successfully open a dialog panel and return the name of the file chosen. I am trying to separate core from gui stuff. Also I don't want to rewrite the open file code every project. However, that is simple code, I am mostly trying to grasp the concept for the future. I did not use the document template, but that is another thread. I am going to go through the books I have before actually writing an app, but I do need to type code in and see it work before I can continue. It helps it sink in.

So here is what I am thinking, which may be wrong. I have a class(TNTFileController) that has a method (-(void)chooseFile) to display a file dialog and set the instance variable fileName.  When the choose button is clicked (- (IBAction)chooseFile:(id)sender) I want to create an instance of (TNTFileController) and execute the method (-(void)chooseFile). I have this, but believe it to be wrong:

id theFile = [[TNTfileController alloc] init];


Todd






regular - member
54 posts

I have it working. A gui class to do the IBAction stuff and a class that has an open file dialog method and sets a text field to the result.

I have a question about the following:

id theFile = [[TNTfileController alloc] init];


I used id to cover everything. What would be the correct type? NSString? What if one of the instance variables is an integer, like filesize. And do I need to declare theFile in the header file? I did not and everything works. But all other vars are declared so it seems wrong.

Thank you,
Todd

back to reading...............

superstar - member
233 posts





Todd
You're doing good.  And sure, you have to type and code to get this into your brain.  Reading the book provides ideas, however typing and building the code is how you learn and remember.

Don't use id for anything.  Use the class and then the compiler can help you with warnings.  So do:

TNTFileController* theFile = [[TNT...]init];

[theFile choooooseFile] ; // compiler will say "I'm not sure TNTFileController has method "choooooseFile"

Try to keep you classes in separate files (so you have TNTFileController.mm and TNT..h and possibly TNTFileController.xib).  Always use .mm (=ObjC++, .m=Obj/C).  One day soon, you'll start another project and say "ah, I can use that class again". Right, if you keep all your projects in a directory "~/Projects", then move your common classes to ~/Projects/myLibrary".  When you move something into ../myLibrary, you have to change the existing project to #import "../myLibrary/TNTFileController.h" (and modify the projects reference to TNTFileController.mm).  1 minute and you have a sharable class.  You enhance the class and every app gets the same new bugs added for free!

I'm not sure I've answered all your questions.  If you'd like to talk on the phone or Skype - email me at robin@clanmills.com and let's fix a time.  I'm in San Jose, California.  You can VNC to my machine if you would like me to demo something.  This stuff is fun and cool.  However it's not intuitive and a little help and encouragement will make the journey to enlightenment more rewarding and quicker.


__________________
regular - member
54 posts

Thank you very much for the reply. You answered much of my question together with reading it finally clicked. I am a php programmer so know most of it, it is just putting into a cocoa context. I am more of a procedural minded guy so my head swims sometimes. Other times I just overthink it.

I agree this is fun a cool! I have always wanted to program my mac, for 15 or so years now I have "dabbled" with some simple applescripts and Applescript studio, but this is going to great... thanks again for the help and also an apology, once it clicked it was a really simple problem.

Thanks again,
Todd

Page 1
1–6

Locked Topic


You must be a member to post in this forum

Join Now!