Can a presented view controller also be a presenting view controller?
Asked Answered
H

5

11

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.

For a) I am doing this:

[[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.

However in order for b) to appear I first have to dismiss the first view controller using:

   [[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. 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. 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:

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.

However as mentioned earlier on, if I don't dismiss the first view controller the SMS controller will not appear. So my main question is how can I present the SMS controller on top of the first view controller?

Hussar answered 23/4, 2012 at 21:11 Comment(0)
V
14

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];
Vip answered 12/1, 2013 at 15:50 Comment(5)
By Above second method is there any change for reject app from Appstore..?Rizo
Why would the app store care if you are presenting/dismissing view controllers?Vip
Cause normally we can only present one viewController at a time, But i can can achieve what i want is by using your technique.Rizo
That's not true. It's normal and acceptable to present view controllers on top of other presented view controllers.Vip
When you dismiss secondController, however, pvc is also dismissed leaving rvc visible. Is there a way to dismiss secondController without also dismissing pvc?Lizettelizotte
S
1

I just tried it on iOS15. Yes a presented VC can present another VC.

So suppose you have:

VC1 --> present--> VC2

you can easily call present(VC3(), animated: true, completion: nil) on VC2 and things would work fine. You can happily end up with:

VC1 --> present--> VC2 --present--> VC3

FWIW when you dismiss VC3, it will only go back to VC2. It won't go back to VC1.

Slice answered 2/2, 2022 at 2:14 Comment(0)
B
0

iOS does not allow you to open two modal views at the same time. The modal view is designed to be the topmost view.

Becki answered 23/4, 2012 at 21:12 Comment(1)
However in iOS 5 (which I'm using) "modal" is now more of a legacy terminology and note how I'm using presentViewController and not presentModalViewController. Is it the case that a presented view controller can not in turn be a presenting view controller?Hussar
C
0

In my case, I have access directly to the presented view controller so in that case:

self.present(viewControllerToPresent, animated: true) {
    //It's presented.
}
Carmelocarmen answered 23/4, 2018 at 5:54 Comment(0)
S
0
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)
Saleh answered 23/1, 2023 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.