views:

28

answers:

2

Right, this may not be possible but maybe it it...

I have 3 UIViewControllers, each with their own XIB files which contain the layout for each slide of a presentation (each slide will contain video, images, other interactive elements). I have hooked them all up to a UIScrollView by adding them one by one, see below:

page1 *vc = [[page1 alloc] initWithNibName:@"page1" bundle:nil];
vc.view.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
[sv addSubview:vc.view];
[vc release];
page2 *vc2 = [[page2 alloc] initWithNibName:@"page2" bundle:nil];
vc.view.frame = CGRectMake(1024.0f, 0.0f, 1024.0f, 768.0f);
[sv addSubview:vc2.view];
[vc2 release];
page3 *vc3 = [[page3 alloc] initWithNibName:@"page3" bundle:nil];
vc.view.frame = CGRectMake(2 * 1024.0f, 0.0f, 1024.0f, 768.0f);
[sv addSubview:vc3.view];
[vc3 release];

What I want to be able to do is create an array of UIViewController and loop around the array to add them, rather than one by one. First off, I can't seem to create an array of UIViewControllers, my code is as follows:

NSArray *pages = [[NSArray alloc] initWithObjects:page1, page2, page3, nil];

and the error is: expected expression before 'page1'.

I did manage to load up the UIViewControllers with the following code:

for (int i = 1; i <= 3; i++) {
    UIViewController *vc = [[UIViewController alloc] initWithNibName:[NSString         stringWithFormat:@"page%i", i] bundle:nil];
    vc.view.frame = CGRectMake((i-1) * 1024.0f, 0.0f, 1024.0f, 768.0f);
    [sv addSubview:vc.view];
    [vc release];
}

But even though the content from the XIBs was displayed, the viewDidLoad function of the loaded in UIViewControllers is never fired.

So, does any one have any tips here for loading up an array of UIViewControllers into a UIScrollView?

Thank you!

A: 

What is page1 *vc? Is page1 the name of your custom view controller class? It should start with a uppercase letter, i.e. Page1ViewController *vc.

Your array needs instances of the classes, not the classes itself.

NSArray *pages = [[NSArray alloc] initWithObjects:vc, vc2, vc3, nil];

But then, you shouldn't release the view controllers until you added them to your pages array, i.e. put [vc release]; [vc2 release]; etc. after your NSArray *pages = ...;

Eiko
A: 

Ahh, of course. Right I now have my view controller array and now i need to loop around them, to set the views position in the UIScrollView and add them as a sub view to the UIScrollView.

for (int i = 0; i < 3; i++) {
    pages[i].view.frame = CGRectMake(i * 1024.0f, 0.0f, 1024.0f, 768.0f);
    [sv addSubview:pages[i].view];
}
[vc1 release];
[vc2 release];
[vc3 release];

does pages[i] need converting/casting to the correct type for this to work? Currenty I get the error: struct NSArray' has no member named 'view'.

Thank you for your reply!

Darthtong