Rahul Vyas
Rahul Vyas

Reputation: 28720

how to add a simple new UIView in a window based application programmatically in iphone?

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?

Upvotes: 2

Views: 8223

Answers (1)

ullmark
ullmark

Reputation: 2479

In most cases you use a ViewController. But it is possible to add a 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 a referens to the window (if you don't already have one) with

[[UIApplication sharedApplication] keyWindow];

Upvotes: 9

Related Questions