4

I am new to programming for the iPhone and this will be my first question here. I have experience with different languages like php/java/c++.

My question is about ViewControllers and views in iOS.

I have started a project which will contain several different things like a login screen, a main screen and several other screens. The goal of this project is to learn how to create everything programmatically instead of using interface builder to get more accustomed to the system. I am using the book: "Advanced iOS 4 Programming" to help me.

I have been able to create all the screens ( and stuff like logging-in is working ), but I am not sure if I did it correctly.

All of my code for creating the textfields/labels/buttons is now located in the ViewController while the main view where everything is put on is almost empty, with nothing being done in it. Shouldn't the code to create the textfields and other components be located in the view itself, or is this the correct approach?

I have looked at several different examples but most use interface builder. The book itself is also not very clear in this matter.

Thanks in advance.

Kind regards, Jasper

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
Jasper
  • 175
  • 1
  • 1
  • 7
  • "The goal of this project is to learn how to create everything programmatically instead of using interface builder to get more accustomed to the system." If you want to learn the system you should be learning how to use Interface Builder. Far too many first-time iPhone developers are afraid of IB, when they should be embracing it. – kubi Feb 26 '11 at 17:25
  • @Joe i dont think that you need to use IB at all in objective c(while building the iphone app) or that you should be a master of IB first to get the good hold of the language.Its a matter of choice to use either one according to your requirement. In fact the programmers from different platform are good at handling the views from the code. IB has its own good side and he just don't know it yet, but atleast he is trying to learn something and dont you think we should encourage him in that. – Robin Feb 26 '11 at 17:41
  • @Joe if you take a look at brad larson's app molecule, he did not use IB at all. its a small screen you can't do much. just some basic stuff. – Robin Feb 26 '11 at 17:47
  • Hi Guys! I tried starting with interface builder, but I had no clue what I was doing connecting all the objects together. This was also because I didn't understand the basic concepts with the views and viewcontrollers. Writing code helps me understand the underlying system so I can (in the future) use interface builder as it will be more clear what I'm doing then. – Jasper Feb 26 '11 at 20:00

2 Answers2

3

In the view you have the view - on other words, literally what the human user sees with their eyeballs.

So, for example, if you are doing complicated drawing you will have your own custom drawRect: method, and that for example is in the view.

On the other hands ......

In the view controller you have things that control the view.

Generally speaking "everything" goes in the view controller.

When you first start programming for iPhone (or Mac), simply put everything in the view controller and don't worry too much. There's plenty to learn. OK?

Eventually, separate out the "actual drawing" separately in to the view.

Hope this simple explanation for beginners helps!

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Hello Joe, Thanks for your reply. What do you mean by 'actual drawing' ? (does this also mean drawing the textboxes and stuff on the screen or do you mean drawing with quartz )? – Jasper Feb 26 '11 at 19:55
  • Thanks Joe, this makes things clear. Because I thought the custom UIView files were a replacement of the xib files I was under the impression that all the components needed to go in the UIView file. – Jasper Feb 26 '11 at 20:07
1

In simple controller code should contain methods like...

class myLoginController : NSObject
{
UIView *myView;
}

-(void) initLoginController
-(void) loadLoginViewInView :(UIView*)inView;
-(void) removeLoginView;
-(void) isViewLoaded;
-(void) submitButtonClicked : (id) button;
-(BOOL) isLoginSuccess;

and initLoginController you can create your view,

-(void) loadLoginViewInView :(UIView*)inView
{
[inView addSubview:myView];
}

and in removeLoginView you can remove "myView" from its superView .

Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63