instantiateViewControllerWithIdentifier似乎调用了viewdidload [英] instantiateViewControllerWithIdentifier seems to call viewdidload

查看:266
本文介绍了instantiateViewControllerWithIdentifier似乎调用了viewdidload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式将ViewController推送到导航控制器,而我正在使用我的故事板创建它。

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

这是我的代码:

+ (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];
}

GenericViewController viewDidLoad 我正在使用我的项目。感谢一些断点,我看到 GenericViewController viewDidLoad juste在 instantiateViewControllerWithIdentifier 之后带有 items 等于nil。

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.

我认为在<期间调用 MyViewController viewDidLoad code> pushViewController 方法。

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

任何想法为什么 viewDidLoad 在调用期间被调用 instantiateViewControllerWithIdentifier

Any idea why viewDidLoad is called during instantiateViewControllerWithIdentifier ?

---更新:---

这里我的 viewDidLoad

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

    [super viewDidLoad];


    [...]
}

self.items 为零。所以什么都没做。

self.items is nil. so nothing is done.

推荐答案

给任何可能仍有同样问题的人。

To anyone that might still have the same problem.

我有类似的情况并通过调用loadViewIfNeeded()来解决它。

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.

这篇关于instantiateViewControllerWithIdentifier似乎调用了viewdidload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆