Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iOS:正确地将视图引入已可见的父级_C#_Ios_Xamarin - Fatal编程技术网

C# iOS:正确地将视图引入已可见的父级

C# iOS:正确地将视图引入已可见的父级,c#,ios,xamarin,C#,Ios,Xamarin,我有以下代码将子视图控制器引入父视图控制器: public static void AdoptViewController(this UIViewController parent, UIViewController child) { parent.AddChildViewController(child); parent.View.AddSubview(child.View); child.DidMoveToParentViewController(parent); }

我有以下代码将子视图控制器引入父视图控制器:

public static void AdoptViewController(this UIViewController parent, UIViewController child)
{
    parent.AddChildViewController(child);
    parent.View.AddSubview(child.View);
    child.DidMoveToParentViewController(parent);
}
这通常可以正常工作,除非父视图已经可见。在这种情况下,子视图控制器不会收到
视图将出现
事件。这在我的应用程序中造成了连锁反应

我发现,这意味着这样做:

public static void AdoptViewController(this UIViewController parent, UIViewController child)
{
    parent.AddChildViewController(child);

    child.BeginAppearanceTransition(true, false);
    parent.View.AddSubview(child.View);
    child.EndAppearanceTransition();

    child.DidMoveToParentViewController(parent);
}
但是,这会导致在父对象尚不可见的情况下,
视图将出现两次
事件。我对代码进行了一点修改:

public static void AdoptViewController(this UIViewController parent, UIViewController child)
{
    parent.AddChildViewController(child);

    // could also use ViewIfLoaded for later iOS versions
    var parentAlreadyVisible = parent.IsViewLoaded && parent.Window != null;

    if (parentAlreadyVisible)
    {
        child.BeginAppearanceTransition(true, false);
    }

    parent.View.AddSubview(child.View);

    if (parentAlreadyVisible)
    {
        child.EndAppearanceTransition();
    }

    child.DidMoveToParentViewController(parent);
}
这似乎工作得很好,但现在我的
viewdidappeage
事件在子视图中触发了两次!第一次是由
EndAppearanceTransition
触发的,第二次似乎是由iOS本身触发的。这实际上并没有给我带来任何问题,但还是令人不安


我如何编写一个独立的
AdoptViewController
实用程序方法,正确地将子视图控制器引入父视图,并以正确的次数引发正确的事件?

在iOS 10.3模拟器上,您的第一个变体适用于我。我要在孩子身上出现。这可能取决于您如何实例化您的孩子?我是从Storyboard获得的我感觉到您可能正在添加子视图控制器的新实例,您可能没有注意到,因为您正在将相同的布局应用于要添加到屏幕上的视图。这是我在写这个例子时无意中做的第一件事。我意外地实例化了两个实例,这导致调用加倍。尝试将日志语句放入正在添加子视图控制器的方法中,然后查看发生了什么。我在swift@AntonTheDev中给出了一个要点作为例子,谢谢你的建议。但是,我刚刚检查过,肯定只创建了一个子实例。@KentBoogaart尝试在根类中放入一个print语句,也可能是内存泄漏