Take the 2-minute tour ×

I am looking for a tutorial on how to create a complex view in code.

For example, a background image to the view, multiple sub views with buttons.

Is there any tutorials on how to complete this?

share|improve this question
What have you tried? Really, there's no such thing as a tutorial on how to create a "complex view". Either you know how to make views and put subviews in them or you don't. If you can do just one, you can extend that to be as complex as you want. –  matt Feb 4 '13 at 3:46
add comment

closed as not a real question by Kurt Revis, matt, Vin, Jai, alxx Feb 4 '13 at 6:47


2 Answers

You should read about View Controller Programming Guide in iOS and perhaps the UIView documentation.

It is possible to give a UIView object several subviews by using the

- (void)addSubview:(UIView *)view

method of a UIView object.

share|improve this answer
add comment

you can use addSubview:

UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,200)];
UIImageView *imgV =[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
UIButton *btn1 =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100,75)];

imgV.image = [UIImage imageNamed:@"background.png"];
[btn1 setTitle:@"Btn" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:imgV];
[view addSubview:btn1];

[self.view addSubview:view];

this will create one view with background image and a button, if you understand the concept then you can add as many objects as you want.

share|improve this answer
If I create a button in code, do I not need the (IBAction) part in header file, and the relevant method for that button? –  StuartM Feb 3 '13 at 22:39
like I did,, [btn1 addTarget:self action:@selector(myAction:)... you should create a method -(void)myAction:(id)sender{} that will receive the action from btn1. –  Aziz Feb 3 '13 at 23:15
It is not required to add a IBOutlet property in the interface declaration. Furthermore, it is not required to add a IBAction method there. It is not even required to add a method declaration somewhere, you can just define the method in the @implementation part. The declaration is just a good style, either in a public or private @interface –  who9vy Feb 4 '13 at 11:22
add comment
