简体   繁体   English

如何以编程方式选择嵌入式视图的控制器?

[英]How to programmatically choose an embedded view's controller?

I have an embedded container on a view controller, and I would like to change it's content depending on a specific condition. 我在视图控制器上有一个嵌入式容器,我想根据特定条件更改其内容。 How should I do this, knowing that there can only be one embed segue linked to an embedded container. 知道只能有一个嵌入到链接到嵌入式容器的嵌入序列,我该怎么做。

I tried to put a view controller between my embedded container and my 2 possible content views, but it won't work because of custom segues (error : "Could not create a segue with class 'null'). I don't understand this error by the way, if someone could tell me more about it :) 我试图在我的嵌入式容器和2个可能的内容视图之间放置一个视图控制器,但由于自定义设置而无法正常工作(错误:“无法使用类'null'创建一个设置)。我不理解这一点。顺便说一句错误,如果有人可以告诉我更多有关它的信息:)

I read about some ways to go around this problem by creating a tab view, and switching between the tabs programmatically, or by adding 2 container view and hiding the unwanted one, but these seem to be kind of hacky. 我了解了一些通过创建选项卡视图,以编程方式在选项卡之间进行切换,或者通过添加2个容器视图并隐藏不需要的视图来解决该问题的方法,但是这些方法似乎有些怪异。

What would be the best practice to do this ? 最佳做法是什么? (In swift please) (请迅速)

Thank you for your help 谢谢您的帮助

There's two ways to do it, the first is to add two container views on top of each other and set the alpha to 0 for one and 1 for the other and switch the alpha values when you want to change between view controllers. 有两种方法,一种是在彼此之上添加两个容器视图,并将一个的alpha设置为0,将另一个设置为1,并在要在视图控制器之间进行切换时切换alpha值。 The disadvantage of this is that there will always be two view controllers instantiated. 这样做的缺点是将始终有两个实例化的视图控制器。 The second way is to change the the type of segue from embed to a custom segue (this will allow you to add more than one segue in the storyboard) that loads or swaps a view controller. 第二种方法是将序列的类型从嵌入更改为自定义序列(这将允许您在情节提要中添加多个序列),以加载或交换视图控制器。 Here is the implementation of a segue I implemented that does this, if you can understand it you can implement it in swift. 这是我实现的segue的实现,如果可以理解,则可以快速实现。

 (void)perform
//
// Used to seque between the master view controller and its immediate child view controllers and also from the homw view controller
// and all its immediate child controllers.
// At app launch then it is necessary to directly load a particular view controller - this is determined by checking that the source
// view controller has no children. At other times the seque is used to switch from one controller to another.
//
{

    //
    // This seque is for use when there is a container view controller where it is necessary to switch the contained view controllers (storyboards only permit one embed seque).
    //
    //
    //                             embed segue                              segueA
    //             MainVC  --------------------------> ContainerVC -------------------> VCA
    //     (has the view containing
    //       the containded VC)
    //                                                                      sequeB
    //                                                             --------------------> VCB
    //
    //
    // When the app initially launches the OS will automatically execute the embed seque and thus the ContainerVC gets the opportunity in its viewDidLoad to decide which
    // VC to load at that point and then execute either segueA or sequeB. Assuming it calls sequeA then when the seque executes the source will be the ContainerVC and the
    // destination with will VCA. The seque checks to see if the containerVC already has any children, if not then it knows to just add VCA as a child.
    //

    DDLogInfo(@"SEGUE - entered seque");
    UIViewController *container = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;
    if([container.childViewControllers count] == 0)
    {
        DDLogInfo(@"SEGUE - adding intitial VC: %@", [destination description]);
        // The containerVC doesn't yet any any children so just add the destination VC as a child
        [container addChildViewController:destination];
        destination.view.frame = container.view.frame;
        [container.view addSubview:destination.view];
        [destination didMoveToParentViewController:container];
    }
    else
    {
        // The containerVC already has an existing child and thus it is necessary to swap it out and replace it with the new child
        UIViewController* currentChild = container.childViewControllers[0];

        currentChild.view.userInteractionEnabled = NO;
        PyngmeAssert([container.childViewControllers count] == 1, @"More than one child controller");

        // First check to make sure the destination type is not the same as the current child
        if([destination isMemberOfClass: [currentChild class]])
        {
            DDLogInfo(@"SEGUE: Trying to swap view controllers of the same type *****");
        }
        else
        {
            // Swap the new VC for the old VC
            destination.view.frame = container.view.frame;
            [currentChild willMoveToParentViewController:nil];
            [container addChildViewController:destination];

            [container transitionFromViewController:currentChild
                                   toViewController:destination
                                           duration:0.35
                                            options:UIViewAnimationOptionTransitionCrossDissolve
                                         animations:^{
                                         }
                                         completion:^(BOOL finished) {
                                             [currentChild removeFromParentViewController];
                                             [destination didMoveToParentViewController:container];
                                             DDLogInfo(@"SEGUE finished swapping seque");
                                         }];
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象从视图传递到嵌入在作为容器视图的Tabbar控制器中的Navigationcontroller中的视图? - How to pass an object from a view to a view that's embedded in a Navigationcontroller that's embedded in a Tabbar controller that's a container view? 以编程方式加载View Controller的插座视图 - Loading View Controller's Outlet view Programmatically 如何从嵌入式容器视图的视图引用视图控制器的标题? - How do I refer to the title of a view controller from an embedded container view's view? 以编程方式从导航控制器中嵌入的视图中消除弹出窗口 - Programmatically dismiss popover from view embedded in navigation controller 如何以编程方式更改为视图控制器? - How to change to a view controller programmatically? 如何以编程方式调用View Controller? - How to call a View Controller programmatically? 以编程方式访问导航控制器的主视图 - Access a Navigation controller's main view programmatically 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 如何在视图控制器之外以编程方式创建视图 - How to create view programmatically outside a view controller 如何以编程方式从不相关的View Controller调用View Controller? - How to call a View Controller from not related View Controller programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM