开发者

What goes in the View and what goes in the View Controller?

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 tex开发者_JAVA技巧tfields 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


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!


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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜