Question

What I'd like to do is create a navigation controller based on a layout. Therefor I created a method renderLayout in my UIStoryboard class.

renderLayout:

UINavigationController *navigationController = [[UINavigationController alloc] init];
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];

/* navigation bar styling */
// navigationController.toolbar.translucent = false;
// navigationController.title = @"TEST";

UIViewController *controller = [[UIViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
scrollView.contentSize = CGSizeMake(1024, /* some calculated value here */);

/* controller styling */
//controller.title = [page valueForKey:@"title"];

NSArray *items = /* ... */

int containerHeight = (1 + ([items count] / 3)) * 29;
MultiSelectionViewController *multiSelection = [[MultiSelectionViewController alloc] initWithItems:[data valueForKey:@"items"]];
multiSelection.view.frame = CGRectMake(20, 65, 984, containerHeight);
/* for debugging */
//multiSelection.view.backgroundColor = [UIColor redColor];

[scrollView addSubview:multiSelection.view];
[controller addChildViewController:multiSelection];
[multiSelection didMoveToParentViewController:controller];

[view addSubview:scrollView];
[controller.view addSubview:view];
[viewControllers addObject:controller];

[navigationController pushViewController:controller animated:true];
return navigationController;

The MultiSelection is a UICollectionViewController<UICollectionViewDataSource> kind of class that creates a collection of items which can be selected.

MultiSelectionViewController init method:

- (id)initWithItems:(NSArray*)items
{
    self = [super init];
    if (self) {
        self.items = items;

        UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
        self.view = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, 984, 29) collectionViewLayout:layout];

        self.collectionView.dataSource = self;
    }
    return self;
}

After a uses pushes a button on that current UIViewController the method gets called. It should then construct the navigation controller which I return and later load with

[self presentViewController:renderedNavController animated:true completion:^(){}];

So far everything is working quite well. The one problem I have is that It displays all kinds of labels and buttons and stuff that I can directly push onto the scrollView but it fails to load the MultiSelection elements. It displays a red rectangle in the size of the view frame but viewDidLoad never gets called inside the MultiSelectionViewController. Can someone help me with this? I've already done a lot of google research but couldn't find any solution yet. I hope I explained my problem thoroughly, if not, ask away :)

Was it helpful?

Solution

Okay I finally found the answer. In order to have the viewDidLoad method called I have to provide a layout and init the view controller with it.

Hence self = [super init]; is wrong, the correct way to do it is self = [super initWithCollectionViewLayout:layout

On a side note, I had to use UICollectionViewFlowLayout (instead of UICollectionViewLayout) and use the method setItemSize to provide the correct size of the cells.

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