如何在子視圖控制器中顯示segue ? [英]How to show a segue inside

I have a main view controller, inside this view controller I show another view controller as its child view controller. The code to show the child view controller as shown below. The self.currentController is the child controller which will be located inside the main controller.


我有一個主視圖控制器,在這個視圖控制器中我顯示另一個視圖控制器作爲它的子視圖控制器。顯示子視圖控制器的代碼如下所示。自我。currentController是將位於主控制器中的子控制器。


        self.addChildViewController(self.currentController!)

        self.currentController?.view.frame = self.operationView.bounds

        self.currentController?.view.layoutIfNeeded()

        self.operationView.addSubview((self.currentController?.view!)!)

        self.setNeedsStatusBarAppearanceUpdate()

Now I want to perform a show segue (another controller, let call it ThirdController) inside the child view controller by using below code:


現在我想在子視圖控制器中執行一個show segue(另一個控制器,我們稱它爲ThirdController),使用下面的代碼:


performSegueWithIdentifier("ShowSegue", sender: nil)

on doing this, the ThirdController will fill on the full screen. What I want to do is to show the third controller on the child controller place. How can I do this?


在此過程中,ThirdController將填充整個屏幕。我要做的是顯示子控制器位置上的第三個控制器。我該怎麼做呢?


2 個解決方案

#1

1  


OK, Sorry I don't know how to write the answer in Swift. So I'll just show you how my solution is done in Objective-C.


好的,對不起,我不知道怎麼用Swift寫答案。我將向你們展示如何用Objective-C求解。


Code to load the first subview:


加載第一個子視圖的代碼:


- (void)loadASubview

{

    subview = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];


    [self addChildViewController:subview];

    [self.view addSubview:subview.view];


    [subview didMoveToParentViewController:self];

    [subview.view setFrame:self.view.bounds];

}

Code to unload the subview:


卸載子視圖的代碼:


- (void)unloadASubview

{

    [subview willMoveToParentViewController:nil];

    [subview.view removeFromSuperview];

    [subview removeFromParentViewController];

}

Initially, when I need to load subview A, I will simply call loadASubview. After that, if I need to load another subview, I will unload the subview I previously loaded by calling unloadASubview before loading the new subview.


最初,當我需要加載子視圖A時,我只需調用loadASubview。之後,如果我需要加載另一個子視圖,我將在加載新子視圖之前調用unloadASubview卸載之前加載的子視圖。


Please take note that the "subview" variable inside the functions are declared outside.


請注意,函數內的“子視圖”變量在外部聲明。


I hope this will help you.


我希望這能對你有所幫助。


#2

0  


Swift 4


斯威夫特4


func loadASubView(){

    subview = self.storyboard?.instantiateViewController(withIdentifier: "childviewstoryboardid")

    self.addChildViewController(subview!)


    self.containerView.addSubview(subview!.view)

    subview?.didMove(toParentViewController: self)

    subview?.view.frame = self.containerView.frame


}


func unloadASubview(){

    subview?.willMove(toParentViewController: nil)

    subview?.view.removeFromSuperview()

    subview?.removeFromParentViewController()


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章