弹出视图控制器,然后同时关闭以前的视图控制器

时间:2016-12-01 17:32:52

标签: ios objective-c uiviewcontroller uinavigationcontroller

我的主视图控制器A呈现视图控制器B,然后视图控制器B将视图控制器C推到其(B' s)导航堆栈上。现在,从C开始,我希望能够直接进入A.如果我从C运行dismissViewController,动画不顺畅,如果我将C弹出到B然后解除B,则有两个独立/不同的动画,我不想要。

在没有中间过渡的情况下,我有没有办法直接从C到A?

因此,运行以下(从C开始)显示突然转换

[self dismissViewControllerAnimated:YES completion:nil];

而运行以下(从C)显示两个单独的转换...

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

3 个答案:

答案 0 :(得分:2)

使用popToViewController:animated:

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

或者,使用setViewControllers:animated:

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

答案 1 :(得分:2)

如果您的主控制器是您的根控制器,您可以使用

[self.navigationController popToRootViewControllerAnimated:YES];

答案 2 :(得分:0)

为此创建了一个演示,“如果我从C运行dismissViewController,动画就不顺畅”,发现动画很流畅。

视图控制器(Swift)

class ViewController: UIViewController {

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

故事板(源代码)

<?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>