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.
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
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?
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.
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.
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];
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.
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];
}
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.
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.