instantiateViewControllerWithIdentifier seems to call viewdidload

北城以北 提交于 2019-12-07 02:02:38

问题


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.


回答1:


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.




回答2:


See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewDidLoad

viewDidLoad is called when the controller's view is loaded into memory.

viewWillAppear Notifies the view controller that its view is about to be added to a view hierarchy. I think this is when [self.navigationController pushViewController:viewController animated:YES]; is called.

viewDidAppear Notifies the view controller that its view was added to a view hierarchy. This is when MyViewController is added into navigationController.




回答3:


You can use [vc loadViewIfNeeded] in Objective-C or vc.loadViewIfNeeded() in Swift.

apple: loadViewIfNeeded




回答4:


As voyage11 mentioned, viewDidLoad is called when the controller's view is loaded into memory, although someone has pointed out that this does not necessarily occur during the instantiateViewControllerWithIdentifier method.

Following your example code, I'd put the loop in the viewWillAppear method:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];  // good practice to call the super
    for (MyItem *currentItem in self.items[0]) {
        [Do Something]
    }
}


来源:https://stackoverflow.com/questions/23474339/instantiateviewcontrollerwithidentifier-seems-to-call-viewdidload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!