Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instantiateViewControllerWithIdentifier seems to call viewdidload

I am trying to push a ViewController programmatically into a navigation controller, And I'm using my storyboard to create it.

here is my code :

+ (void) pushViewController:(NSString *) identifier ForItems:(NSMutableArray *) items sender:(UIViewController *) sender {
    GenericViewController *viewController = (GenericViewController *)[sender.storyboard instantiateViewControllerWithIdentifier:identifier];

    viewController.items = [[NSMutableArray alloc] init];
    [viewController.items removeAllObjects];
    [viewController.items addObject:[[NSMutableArray alloc] init]];
    [viewController.items[0] addObjectsFromArray:items];

    [sender.navigationController pushViewController:viewController animated:YES];
}

In GenericViewController viewDidLoad I'm using my items. Thanks to some break points I've seen that GenericViewController viewDidLoad juste after the instantiateViewControllerWithIdentifier with an items equal to nil.

I thought that MyViewController viewDidLoad is called during the pushViewController method.

Any idea why viewDidLoad is called during instantiateViewControllerWithIdentifier ?

---Update :---

Here my viewDidLoad

- (void)viewDidLoad
{
    for (MyItem *currentItem in self.items[0]) {
        [Do Something]
    }

    [super viewDidLoad];


    [...]
}

self.items is nil. so nothing is done.

like image 361
Thomas Besnehard Avatar asked May 05 '14 13:05

Thomas Besnehard


1 Answers

To anyone that might still have the same problem.

I had a similar situation and solved it by just calling loadViewIfNeeded().

let sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = sb.instantiateViewControllerWithIdentifier("LoadingView") as! LoadingViewController
controller.loadViewIfNeeded()

I could then access everything inside the view.

like image 115
Deivi Taka Avatar answered Nov 15 '22 23:11

Deivi Taka