Question

I'm having a main view controller containing a UIScrollView called containerScrollView. This scrollview has on each page another scrollview with the size of the screen containing two view controllers: MessagesViewController and InfoViewController. Here's a schema.

Schema of my app

The personScrollView in the containerScrollView works fine but the problem is in the adding of the two view controllers' view to the personScrollView.

@property (nonatomic, retain) MessagesViewController *matchesVC;
@property (nonatomic, retain) InfoViewController *standingsVC;


for (int i = 0; i < 3; i++) {
    UIScrollView *personScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i*320, 0, 320, self.containerScrollView.frame.size.height)];
    NSArray *colors = @[[UIColor blueColor], [UIColor orangeColor], [UIColor greenColor]];
    [personScrollView setBackgroundColor:[y objectAtIndex:i]];
    [personScrollView setPagingEnabled:YES];
    [personScrollView setContentSize:CGSizeMake(self.view.frame.size.width * 2, personScrollView)];
    [self.containerScrollView addSubview:personScrollView];

    /* Populate the scrollview */
    // Messages
    if (self.messagesVC == nil)
    {
        self.messagesVC = [[MessagesViewController alloc] init];
        [self.messagesVC setFrame:CGRectMake(0, 0, 320, self.containerScrollView.frame.size.height)];
    }
    [personScrollView addSubview:self.messagesVC.view];

    // Info
    if (self.infoVC == nil)
    {
        self.infoVC = [[InfoViewController alloc] init];
        [self.infoVC setFrame:CGRectMake(320, 0, 320, self.containerScrollView.frame.size.height)];
    }
    [personScrollView addSubview:self.infoVC.view];
}

[self.containerScrollView setContentSize:CGSizeMake(320*3, self.containerScrollView.frame.size.height)];

The problem is that the two view controllers (messages and info) only get added once, and to the last personScrollView of containerScrollView.

How to get the view controllers added to all of my personScrollViews? Something wrong with the property declaration?

I have read something about this abusing view controllers, but this is the only solution. There is really a lot of code in the two view controllers and I can't add it to my rootviewcontroller.

Was it helpful?

Solution 3

I ended up creating multiple instances of my view controllers and storing them in an array. Not a great solution, but the best I could find.

@property (strong, nonatomic) MessagesViewController *messagesVC1;
@property (strong, nonatomic) MessagesViewController *messagesVC2;
@property (strong, nonatomic) MessagesViewController *messagesVC3;
@property (strong, nonatomic) MessagesViewController *messagesVC4;
@property (strong, nonatomic) MessagesViewController *messagesVC5;
@property (strong, nonatomic) MessagesViewController *messagesVC6;

self.messagesVC1 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC2 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC3 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC4 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC5 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC6 = [[MessagesViewController alloc] initWithData:data];
self.messagesVCArray = @[self.messagesVC1, self.messagesVC2, self.messagesVC3, self.messagesVC4, self.messagesVC5, self.messagesVC6];

MessagesViewController *messagesVC = [self.messagesVCArray objectAtIndex:i];
[messagesVC setFrame:CGRectMake(0, 0, 320, leagueScrollView.frame.size.height)];
[leagueScrollView addSubview:messagesVC.view];

OTHER TIPS

The problem is with your understanding of the difference between view controllers and views. You need to read up on Creating Custom Container View Controllers.

Apple doc says:

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

You create your controllers once, but you want to add theirs views three times, to three different parent views. You can't do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top