簡體   English   中英

呈現視圖 controller 也可以是呈現視圖 controller 嗎?

[英]Can a presented view controller also be a presenting view controller?

在現有視圖之上,我想:a) 向用戶顯示一個屏幕 b) 然后發送 SMS c) 向用戶顯示另一個屏幕。

對於 a)我這樣做:

[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController: firstController animated: NO completion:nil];

對於 b) 我正在做同樣的事情,除了這當然是呈現不同的 vc,一個 MFMessageComposeViewController。

然而,為了 b) 出現,我首先必須使用以下命令關閉第一個視圖 controller:

   [[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:NO completion: nil];

到目前為止,我可以看到第一個視圖出現,然后看到 SMS 撰寫視圖出現。 發送短信后,我這樣做是為了關閉短信撰寫視圖

   [[UIApplication sharedApplication].delegate.window.rootViewController dismissViewControllerAnimated:NO completion: nil];

但是當我嘗試使用 presentViewController 向用戶呈現另一個屏幕時,什么也沒有發生。 我看不出為什么會這樣,有什么我不知道的嗎?

實際上 SMS 視圖之前和之后的屏幕是相同的,只是它們有不同的文本,所以最簡單的步驟順序是:

a) 顯示帶有文本“abc”的視圖 controller b) 顯示 SMS controller c) 發送 SMS 時關閉 SMS controller d) 使用 IBOutlet 更新第一個視圖 controller 中的文本 e) 關閉第一個視圖 controller。

但是,如前所述,如果我不關閉第一個視圖 controller,則不會出現 SMS controller。 所以我的主要問題是如何在第一個視圖 controller 之上顯示 SMS controller?

您可以在另一個關閉后呈現一個:

UIViewController *rvc = [UIApplication sharedApplication].delegate.window.rootViewController;
[rvc dismissViewControllerAnimated:NO completion:^{
    [rvc presentViewController: secondController animated: NO completion:nil];
}];

或者在另一個上面呈現:

UIViewController *rvc = [UIApplication sharedApplication].delegate.window.rootViewController;
UIViewController *pvc = rvc.presentedViewController;  // you may need to loop through presentedViewControllers if you have more than one
[pvc presentViewController: secondController animated: NO completion:nil];

iOS不允許您同時打開兩個模態視圖。 模態視圖旨在成為最頂層的視圖。

就我而言,我可以直接訪問呈現的視圖控制器,所以在這種情況下:

self.present(viewControllerToPresent, animated: true) {
    //It's presented.
}

我剛剛在iOS15上試過。 是的,一個展示的 VC 可以展示另一個 VC。

所以假設你有:

VC1 --> present--> VC2
extension UIViewController{

    /// most top presented view controller
    var presentedTop:UIViewController {
        var ctrl:UIViewController = self;
        while let presented = ctrl.presentedViewController {
            ctrl = presented;
        }
        return ctrl;
    }
}
// call somewhere someCtrl.presentedTop.present(vc, animated:true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM