tags:

views:

1048

answers:

1

i want to add a new View Which Contain Anything in a window based application programmatically could someone tell me a step by step process to do this and also how to add controls on that new view programmatically.

i have created a new view and added a label in that now how do i add a button and i want to change the text of label when i clcik on that button? anyone help me?

+3  A: 

In most cases you use an ViewController. But it is possible to add an UIView directy to the window. Perhaps you can explain more what you want to do.

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[label setFont:[UIFont boldSystemFontOfSize:18]];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:@"Hello"];
[view addSubview:label];
[window addSubView:view];

You can get an referens to the window (if you don't already have one) with

[[UIApplication sharedApplication] keyWindow];
ullmark
plz tell me where i write the code. do i need to add a new uiview subclass?
Rahul Vyas
You would do this in the RootViewController. If you're not used to doing this, you're better off using Interface Builder.
Alex Reynolds
If you want to build an application from scratch with the windowsbased template. I would start by coding a viewcontroller, then build upp the views subviews in the "loadView" function. Then i would add it to the window in the applicationDidFinishLaunching function in the appdelegate... But as Alex says, any reason not to use the IB?
ullmark
Also, my example was done without taking memory management into account. You would need to call [label release] and [view release]... Just to be clear.
ullmark