简体   繁体   English

呈现视图 controller 也可以是呈现视图 controller 吗?

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

On top of an existing view I want to: a) display a screen to the user b) then send an SMS c) display another screen to the user.在现有视图之上,我想:a) 向用户显示一个屏幕 b) 然后发送 SMS c) 向用户显示另一个屏幕。

For a) I am doing this:对于 a)我这样做:

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

and for b) I am doing the same thing, except this is presenting a different vc of course, a MFMessageComposeViewController.对于 b) 我正在做同样的事情,除了这当然是呈现不同的 vc,一个 MFMessageComposeViewController。

However in order for b) to appear I first have to dismiss the first view controller using:然而,为了 b) 出现,我首先必须使用以下命令关闭第一个视图 controller:

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

That works so far, I can see the first view appear then see the SMS compose view appear.到目前为止,我可以看到第一个视图出现,然后看到 SMS 撰写视图出现。 When the SMS is sent I am doing this to dismiss the SMS compose view发送短信后,我这样做是为了关闭短信撰写视图

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

But then nothing happens when I try to present another screen to the user using presentViewController.但是当我尝试使用 presentViewController 向用户呈现另一个屏幕时,什么也没有发生。 I can't see any reason why this should be, is there something I'm not aware of?我看不出为什么会这样,有什么我不知道的吗?

Actually the screen before the SMS view and after it are the same except they have different text, so the easiest sequence of steps would be:实际上 SMS 视图之前和之后的屏幕是相同的,只是它们有不同的文本,所以最简单的步骤顺序是:

a) present the view controller with text "abc" b) present the SMS controller c) when the SMS is sent dismiss the SMS controller d) update the text in the first view controller using an IBOutlet e) dismiss the first view controller. a) 显示带有文本“abc”的视图 controller b) 显示 SMS controller c) 发送 SMS 时关闭 SMS controller d) 使用 IBOutlet 更新第一个视图 controller 中的文本 e) 关闭第一个视图 controller。

However as mentioned earlier on, if I don't dismiss the first view controller the SMS controller will not appear.但是,如前所述,如果我不关闭第一个视图 controller,则不会出现 SMS controller。 So my main question is how can I present the SMS controller on top of the first view controller?所以我的主要问题是如何在第一个视图 controller 之上显示 SMS controller?

You can either present one after the other closes: 您可以在另一个关闭后呈现一个:

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

Or present another on top: 或者在另一个上面呈现:

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 does not allow you to open two modal views at the same time. iOS不允许您同时打开两个模态视图。 The modal view is designed to be the topmost view. 模态视图旨在成为最顶层的视图。

In my case, I have access directly to the presented view controller so in that case: 就我而言,我可以直接访问呈现的视图控制器,所以在这种情况下:

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

I just tried it on iOS15.我刚刚在iOS15上试过。 Yes a presented VC can present another VC.是的,一个展示的 VC 可以展示另一个 VC。

So suppose you have:所以假设你有:

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.

相关问题 使用CAShapeLayer遮罩呈现的视图控制器,也遮罩呈现的视图控制器 - masking presented view controller with CAShapeLayer also mask the presenting view controller 如何暂时隐藏同时呈现视图控制器的呈现视图控制器 - How to temporarily hide a presented View controller that is also presenting a view controller 如何在呈现的View Controller和呈现的View Controller之间建立关系? - How to setup a relationship between the presenting View Controller and the presented View Controller? 呈现视图 controller 位于呈现视图 controller 下方 - Presented view controller place under presenting view controller 旋转呈现的视图并锁定呈现视图控制器的方向 - Rotate presented view and lock the orientation of presenting view controller 为什么模式视图(由详细视图控制器呈现)呈现视图控制器是拆分视图控制器? - Why modal view's (presented by detail view controller) presenting view controller is split view controller? 解除 UIImagePickerController 也解除呈现视图控制器 - Dismissing of UIImagePickerController dismisses presenting view controller also 关闭presentedViewController也会关闭呈现视图controller - Dismissing presentedViewController also close the presenting view controller 调用viewWillAppear时显示视图控制器的问题被解除 - Issue with calling viewWillAppear of presenting view controller when presented one is dismissed UIViewController-将没有Flash的呈现视图控制器切换为呈现一个? - UIViewController - switch presented view controller without flash to presenting one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM