弹出视图控制器,然后同时关闭前一个视图控制器

Posted

技术标签:

【中文标题】弹出视图控制器,然后同时关闭前一个视图控制器【英文标题】:Pop view controller and then dismiss previous view controller simultaneously 【发布时间】:2016-12-01 17:32:51 【问题描述】:

我的主视图控制器 A 呈现视图控制器 B,然后将视图控制器 C 推送到其(B 的)导航堆栈上。现在,从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];

【问题讨论】:

【参考方案1】:

使用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];

【讨论】:

非常感谢@TomSwift。这应该可行,但我的设置中还有另一个问题即将到来 - 我实际上创建了一个新的导航堆栈(从 A 呈现 B),因为我希望导航栏有所不同。所以当我弹出到 A 时,它只是崩溃了,因为 A 不在导航堆栈上!我需要进行相当多的重构,所以稍后可能会再次合并您的反馈。再次感谢!【参考方案2】:

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

[self.navigationController popToRootViewControllerAnimated:YES];

【讨论】:

非常感谢@Flexicoder。害怕 A 不是根视图控制器 - 事实上,根据我对 Tom 的评论,我从 B 启动了一个新的导航堆栈,弹出到根视图控制器只会把我从 C 带到 B!再次感谢!【参考方案3】:

为此创建了一个演示,“如果我从 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"  />
                        <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"  />
                                <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"  />
                        <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"  />
                        <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>

【讨论】:

以上是关于弹出视图控制器,然后同时关闭前一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何同时关闭和弹出视图控制器

如何从关闭命令访问前一个视图控制器

ios:如何关闭模态视图控制器,然后弹出推送的视图控制器

如何从弹出视图中关闭第二个视图控制器

滑动最后一个视图控制器后,如何关闭/弹出 UIPageViewController?

不使用导航控制器时弹出视图控制器?