Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 12:32 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Question Confused about a few things (n00b questions)

Hello everyone,

I'm new to iPhone/object-oriented programming, and after reading/watching multiple tutorials, I still have basic questions while I'm working on my very first application.

1) I have a AppDelegate and a ViewController class... but no View class! I just selected "View-based Application" and it seems to be the default project layout. Why is MainWindow.xib linking to ViewController.xib? Shouldn't ViewController.xib link to a View.xib? I'm asking this because I want to draw stuff on my view, and I read somewhere that I have to put drawing functions in the drawRect: method of the VIEW. Not the view controller. Again please correct me if I'm wrong.

2) Where is the main application code supposed to be? For some reason I started building everything in my view controller class, is this correct or not? It works, but I just ain't sure. The main variables of my program are instance variables of the view controller, the main functions (like load, save, etc.) are its instance methods, etc. The main initialization occurs in the viewDidLoad method. I'm pretty sure it's not the normal way.

Thank you for your help!

Benoit
benoitr007 is offline   Reply With Quote
Old 06-29-2009, 10:32 AM   #2 (permalink)
New Member
 
Join Date: Jun 2009
Location: Brookline, MA
Posts: 14
Default

Quote:
Originally Posted by benoitr007 View Post
Hello everyone,

I'm new to iPhone/object-oriented programming, and after reading/watching multiple tutorials, I still have basic questions while I'm working on my very first application.

1) I have a AppDelegate and a ViewController class... but no View class! I just selected "View-based Application" and it seems to be the default project layout. Why is MainWindow.xib linking to ViewController.xib? Shouldn't ViewController.xib link to a View.xib? I'm asking this because I want to draw stuff on my view, and I read somewhere that I have to put drawing functions in the drawRect: method of the VIEW. Not the view controller. Again please correct me if I'm wrong.

2) Where is the main application code supposed to be? For some reason I started building everything in my view controller class, is this correct or not? It works, but I just ain't sure. The main variables of my program are instance variables of the view controller, the main functions (like load, save, etc.) are its instance methods, etc. The main initialization occurs in the viewDidLoad method. I'm pretty sure it's not the normal way.

Thank you for your help!

Benoit
As to your first question, take the following steps:

1. Subclass UIView, eg, call it MyView --> Then you get two new files, MyView.h and MyView.m
2. You can override UIView's drawRect method in MyView.m
3. In the viewController.xib file in Interface Builder, select the "view" and change the view's class to "MyView."

Result: self.view is now a MyView object, not merely a UIView.

Hope that helps.

Check out some more info and useful links at my blog: iDoTouch.blogspot.com

Dave
iDoTouch is offline   Reply With Quote
Old 06-29-2009, 12:53 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Default

Thanks a lot iDoTouch! I'm now able to draw stuff using the following code in the implementation of my view:

Code:
- (void)drawRect:(CGRect)rect
{
    CGContextRef viewContext = UIGraphicsGetCurrentContext();
    //Drawing random stuff... a red rectangle here
    CGContextSetRGBFillColor(viewContext, 255, 0, 0, 1);
    CGContextFillRect(viewContext, CGRectMake(0, 0, 320, 200));
}
Now I would like to draw a UIImage object. I know there is the drawInRect: instance method, but my UIImage object is an instance variable of my view controller class (where the main code of my project is located). This brings again problem number two, or the question: how can a UIView send a message to its view controller object?
benoitr007 is offline   Reply With Quote
Old 06-29-2009, 11:06 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Default

I realize that perhaps what I'm asking doesn't make much sense for most of you, so please tell me if it is the case. I am very confused when it comes to these basic questions, and it's currently blocking me in my program. Thanks a lot for your help.

PS: What are the rules for bumping topics?
benoitr007 is offline   Reply With Quote
Old 06-30-2009, 03:42 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 39
Default

It is the case XCode is one of the best development environment I've ever seen and it ships with a bunch of well-structured docs.

Try to read about object propertires, look for @synthesize keyword, take a look at the code samples in Xcode...
Artem is offline   Reply With Quote
Old 06-30-2009, 02:26 PM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Default

Well, I know about object properties and do use them, but cannot find a way to send a message to the view controller instance from my view object. There is a viewController instance variable in my AppDelegate object, but I can't retrieve it. I must be stupid, but I have read the documentation and still can't find a way to do that. Thanks for your help.
benoitr007 is offline   Reply With Quote
Old 07-01-2009, 02:13 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 39
Default

You can make your own view constructor like:
Code:
- (id) initWithFrame:(CGRect)frame andController:(SomeControllerClass *)controller {

}
...and save controller as instance variable.
Artem is offline   Reply With Quote
Old 07-01-2009, 07:51 AM   #8 (permalink)
New Member
 
Join Date: Jun 2009
Location: Brookline, MA
Posts: 14
Default

Quote:
Originally Posted by benoitr007 View Post
Well, I know about object properties and do use them, but cannot find a way to send a message to the view controller instance from my view object. There is a viewController instance variable in my AppDelegate object, but I can't retrieve it. I must be stupid, but I have read the documentation and still can't find a way to do that. Thanks for your help.
Without seeing sample code, it's hard to diagnose your issue. But you can access your viewController in the app delegate as follows:

//Get the delegate
MyAppDelegate *delegate=[[UIApplication sharedApplication] delegate];

//get the viewController
[delegate.viewController viewControllerMethod];

Does that help?
iDoTouch is offline   Reply With Quote
Old 07-01-2009, 11:31 AM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Default

Thank you very much for the replies... Unfortunately I'm still confused. Haha.

iDoTouch: Thanks, but actually I can access the viewController in the AppDelegate since it's where the instance is created. What I want is access the viewController in the view object.

Artem: The problem is the view isn't initialized programmatically (as far as I know). I have told IB that the view of my view controller should be an object from the MainView class, but that's it. Everything seems to be done in the background. So where should I put that code? I want to keep my IB-made interface.

Also nobody really answered the question number 2 of my original post.
benoitr007 is offline   Reply With Quote
Old 07-01-2009, 12:00 PM   #10 (permalink)
New Member
 
Join Date: Jun 2009
Location: Brookline, MA
Posts: 14
Default

Quote:
Originally Posted by benoitr007 View Post
Thank you very much for the replies... Unfortunately I'm still confused. Haha.

iDoTouch: Thanks, but actually I can access the viewController in the AppDelegate since it's where the instance is created. What I want is access the viewController in the view object.

Artem: The problem is the view isn't initialized programmatically (as far as I know). I have told IB that the view of my view controller should be an object from the MainView class, but that's it. Everything seems to be done in the background. So where should I put that code? I want to keep my IB-made interface.

Also nobody really answered the question number 2 of my original post.

What I meant to say is that if you put the following code into your MainView.m file, then that is one way to access your view controller.

Say you have a MainView and a MainViewController.
MainViewController's "view" property is of the class "MainView"
In your MainView.m file, one way to access MainViewController (ie, the MainView's controller) is to insert the following code into your MainView.m file:

So, for example, if in my MainView, I want to call a method called "mainViewControllerMethod" of the MainViewController when the touchesEnded method of MainView is called, I would insert the following into MainView.m:

- (void)touchesEndedNSSet *)touches withEventUIEvent *)event
{
//Create a pointer to the delegate then access the mainViewController
MyAppDelegate *delegate=[[UIApplication sharedApplication] delegate];
[delegate.mainViewController mainViewControllerMethod];
}

Does that make more sense?
iDoTouch is offline   Reply With Quote
Old 07-01-2009, 03:15 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
Default

Oh yeah I see now! Thanks a lot for clarifying that.

Views and view controllers still confuse me, but I'll keep reading/watching tutorials and try to master them better.
benoitr007 is offline   Reply With Quote
Old 07-01-2009, 04:24 PM   #12 (permalink)
New Member
 
Join Date: Jun 2009
Location: Brookline, MA
Posts: 14
Default

Quote:
Originally Posted by benoitr007 View Post
Oh yeah I see now! Thanks a lot for clarifying that.

Views and view controllers still confuse me, but I'll keep reading/watching tutorials and try to master them better.
I am glad I could help. In terms of views and view controllers, you will come across a lot of material referring to the programming theory of Model-View-Controller. Speaking from one noob to another, I would recommend not to get bogged down in the details of theory.

And that takes me to your second question, I'd, where to put your main application code. The question itself assumes there is one place to put your code. But there isn't. In object oriented programming, you can put your code anywhere and each object can communicate with other objects.

So where you put you put your code largely depends on what you need it to do.
iDoTouch is offline   Reply With Quote
Old 07-02-2009, 01:53 AM   #13 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 39
Default

As iDoTouch said, you can put your code everywhere. You can even have 50Kb-sized view controller, but.. It will be unmanageble in this case If your app is pretty small that's ok, but try to break-down your code into small logical pieces. For example one class manages all your internet connections, one another manages your database or files, etc. Also, MVC pattern uses Models as main data transfer objects, so all your data should be grouped in such objects. Let controllers do only major backend function calls and some UI things.
Artem is offline   Reply With Quote
Reply

Bookmarks

Tags
confused, view controllers, views

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 250
20 members and 230 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:40 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0