在prepareForSegue完成之前调用的viewDidLoad [英] viewDidLoad called before prepareForSegue finishes

查看:106
本文介绍了在prepareForSegue完成之前调用的viewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为viewDidLoad将在prepareForSegue结束后被调用。这就是Hegarty如何教他的斯坦福大学课程(最近一次是2013年2月)。

I was under the impression that viewDidLoad will be called AFTER prepareForSegue finishes. This is even how Hegarty teaches his Stanford class (as recently as Feb 2013).

然而,今天我第一次注意到viewDidLoad被称为BEFORE之前的准备。完了。因此,我在prepareForSegue中设置的属性不可用于目标viewDidLoad方法中的destinationViewController。

However, for the first time today, I have noticed that viewDidLoad was called BEFORE prepareForSegue was finished. Therefore, the properties that I was setting in prepareForSegue were not available to the destinationViewController within the destinations viewDidLoad method.

这似乎与预期行为相反。

This seems contrary to expected behavior.

更新

UPDATE

我只知道发生了什么事。在我的destinationViewController中,我有一个自定义setter,每次更新model时都会重新加载tableView:

I just figured out what was going on. In my destinationViewController I had a custom setter that would reload the tableView each time the "model" was updated:

DestinationViewController    
- (void)setManagedObjectsArray:(NSArray *)managedObjectsArray
    {
        _managedObjectsArray = [managedObjectsArray copy];
        [self.tableView reloadData];
    }

事实证明,因为destinationViewController是UITableViewController的子类...调用'self.tableView'强制加载视图。根据Apple的文档,调用视图控制器的view属性可以强制加载视图。 UITableViewController的视图是tableView。

It turns out, since the destinationViewController is a subclass of UITableViewController...calling 'self.tableView' forces the view to load. According to Apple's documentation, calling the view property of a view controller can force the view to load. The view of a UITableViewController is the tableView.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

因此,在prepareForSegue中,以下行强制加载destinationViewController的视图:

Therefore, in prepareForSegue the following line was forcing the view of the destinationViewController to load:

vc.managedObjectsArray = <custom method that returns an array>;

为了解决这个问题,我将destinationViewController模型的自定义setter更改为:

To fix the problem, I changed the custom setter of the destinationViewController's model to:

- (void)setManagedObjectsArray:(NSArray *)managedObjectsArray
    {
        _managedObjectsArray = [managedObjectsArray copy];
        if ([self isViewLoaded]) {
            [self.tableView reloadData];
        }
    }

如果tableView打开,这只会重新加载tableView屏幕。因此,在prepareForSegue期间不强制加载视图。

This will only reload the tableView if the tableView is on screen. Thus not forcing the view to load during prepareForSegue.

如果有人反对此过程,请分享您的想法。否则,我希望这可以防止一个人长时间不眠之夜。

If anyone objects to this process, please share your thoughts. Otherwise, I hope this prevents a long sleepless night for someone.

推荐答案

我过去遇到过类似的困惑。我学到的一般经验法则是:

I had run into similar confusions in the past. The general rules of thumb that I learned are:


  1. 在目标VC的viewDidLoad之前调用prepareForSegue

  2. 只有在加载所有出口后才会调用viewDidLoad

  3. 不要尝试在源VC的prepareForSegue中引用任何目标VC的出口。

关于prepareForSegue的另一个经验教训是避免冗余处理。例如,如果您已经通过故事板将tableView单元格转换为VC,则在尝试处理BOTH tableView:didSelectRowAtIndexPath和prepareForSegue时,可能会遇到类似的竞争条件。可以通过使用手动segue或在didSelectRowAtIndexPath处放弃任何处理来避免这种情况。

Another lesson learned with regards to prepareForSegue is to avoid redundant processing. For example, if you already segue a tableView cell to a VC via storyboard, you could run into similar race condition if you attempt to process BOTH tableView:didSelectRowAtIndexPath and prepareForSegue. One can avoid such by either utilizing manual segue or forgo any processing at didSelectRowAtIndexPath.

这篇关于在prepareForSegue完成之前调用的viewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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