![](/img/trans.png)
[英]Dismiss a View Controller and present a new one in the same method - swift
[英]Dismiss and Present same view controller
我有一個視圖控制器,我需要將其關閉並同時顯示。 我曾嘗試關閉它,並回調視圖控制器,但無法正常工作。
[self dismissViewControllerAnimated:YES completion:nil];
UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
ExpandViewController *expandView=
[storyboard instantiateViewControllerWithIdentifier:@"ExpandViewController"];
expandView.delegate=self;
[expandView setEventDict:dict];
[self presentViewController:expandView animated:YES completion:NULL];
在當前顯示的視圖控制器完成關閉之前,您不能顯示視圖控制器。 您將不知道這種情況的發生,直到您要求解雇的completion
處理程序被調用為止。 您的錯誤是completion
處理程序為nil
。 而是提供一個completion
處理程序(在第一行中),該處理程序由代碼的其余各行組成。 因此,他們將在解雇完成后執行。
[self dismissViewControllerAnimated:YES completion:^{
// ... the rest of your code goes in here ...
}];
我不確定您要在問題中尋找什么結果/功能,但是@matt是正確的。 但是,您可能希望無縫地進行此操作。 因此,您可以使用子視圖控制器,而不是使用[self presentViewController:VC animated:animate completion:nil]
方法呈現視圖控制器。
添加子vc:
[self addChildViewController:myVC];
[self.view addSubview:myVC.view];
[myVC didMoveToParentViewController:self];
刪除子vc:
[myVC willMoveToParentViewController:nil];
... remove subview.
您可以在兩個控制器之間設置一個委托,以告訴父級何時關閉視圖以簡化操作。 您還可以使用[self.view insertSubview:myVC atIndex:index]
或其他可能的功能(例如,子視圖上方的insert等)在不同的索引處添加子視圖,以在關閉另一個子視圖之前添加一個子視圖,以提供更無縫的過渡。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.