1
votes

My main view controller A presents view controller B, which then pushes view controller C onto its (B's) navigation stack. Now, from C, I want to be able to go straight to A. If I run dismissViewController from C, the animation is not smooth, and if I pop C to B and then dismiss B, there are two separate / distinct animations, which I do not want.

Is there a way for me to go from C to A directly without the intermediate transition?

So, running the following (from C) shows a sudden transition

[self dismissViewControllerAnimated:YES completion:nil];

whereas running the following (from C) shows two separate transitions...

UIViewController *previousViewController = [self.navigationController.viewControllers 
            objectAtIndex:(self.navigationController.viewControllers.count - 2)];
[self.navigationController popViewControllerAnimated:YES];
[previousViewController dismissViewControllerAnimated:YES completion:nil];
3

3 Answers

2
votes

Use popToViewController:animated:

[self.navigationController popToViewController: self.navigationController.viewControllers.firstObject animated: YES];

Or, use setViewControllers:animated:

NSMutableArray* viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeLastObject];
[viewControllers removeLastObject];
[self.navigationController setViewControllers: viewControllers animated: YES];
2
votes

If your main controller is your root controller you can use

[self.navigationController popToRootViewControllerAnimated:YES];
0
votes

Created a demo for this and "If I run dismissViewController from C, the animation is not smooth" found animations smooth enough.

The View Controller (Swift)

class ViewController: UIViewController {

    @IBAction func dismissAction(_ sender:UIBarButtonItem) {
        if let presentingViewController = navigationController?.presentingViewController as? ViewController {
            presentingViewController.dismiss(animated: true, completion: nil)
        }
    }
}

The storyboard(source code)

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="oLA-Di-cgf">
    <device id="retina3_5" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="n0L-NI-h0u">
            <objects>
                <viewController id="oLA-Di-cgf" customClass="ViewController" customModule="test" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="r6P-JA-nnH"/>
                        <viewControllerLayoutGuide type="bottom" id="ahY-rd-YdT"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="85O-5M-YL1">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="O7h-fZ-9h7">
                                <rect key="frame" x="0.0" y="436" width="320" height="44"/>
                                <items>
                                    <barButtonItem title="Present" id="maa-Qr-JCa">
                                        <connections>
                                            <segue destination="Wca-5m-4jx" kind="presentation" id="CMi-Rw-iRx"/>
                                        </connections>
                                    </barButtonItem>
                                </items>
                            </toolbar>
                        </subviews>
                        <color key="backgroundColor" red="0.41568627450000001" green="0.77647058820000003" blue="0.62745098040000002" alpha="1" colorSpace="calibratedRGB"/>
                        <constraints>
                            <constraint firstItem="ahY-rd-YdT" firstAttribute="top" secondItem="O7h-fZ-9h7" secondAttribute="bottom" id="081-86-3ey"/>
                            <constraint firstItem="O7h-fZ-9h7" firstAttribute="leading" secondItem="85O-5M-YL1" secondAttribute="leading" id="Gxg-mQ-PmM"/>
                            <constraint firstAttribute="trailing" secondItem="O7h-fZ-9h7" secondAttribute="trailing" id="oeG-uG-4Z5"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="gfv-GB-jC7" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="3872" y="-131"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="P5w-Qq-Qnr">
            <objects>
                <navigationController id="Wca-5m-4jx" sceneMemberID="viewController">
                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="gc0-XE-nSj">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <connections>
                        <segue destination="pdW-3L-D3v" kind="relationship" relationship="rootViewController" id="Cmi-b2-qJ7"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="8Iw-y7-6CL" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="4654" y="-131"/>
        </scene>
        <!--View Controller-->
        <scene sceneID="yLb-Gh-kwS">
            <objects>
                <viewController id="pdW-3L-D3v" customClass="ViewController" customModule="test" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="6r9-si-Z7k"/>
                        <viewControllerLayoutGuide type="bottom" id="gn1-Ij-96t"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="GHL-CQ-FOp">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <color key="backgroundColor" red="1" green="0.40000000600000002" blue="0.40000000600000002" alpha="1" colorSpace="calibratedRGB"/>
                    </view>
                    <navigationItem key="navigationItem" id="xWV-p6-Kxr">
                        <barButtonItem key="rightBarButtonItem" title="Dismiss" id="YEp-mM-R0q">
                            <connections>
                                <action selector="dismissAction:" destination="pdW-3L-D3v" id="uIX-Ji-H2L"/>
                            </connections>
                        </barButtonItem>
                    </navigationItem>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="KGz-ra-8TI" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="5458" y="-131"/>
        </scene>
    </scenes>
</document>