2016-12-01 44 views
1

我的主視圖控制器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]; 

回答

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]; 
+0

非常感謝@TomSwift。這應該可以工作,但是我的設置還有另一個問題 - 我實際上已經創建了一個新的導航堆棧(從A展示B),因爲我希望導航欄有所不同。所以當我彈出到A時,它只是崩潰,因爲A不在導航堆棧上!我需要重構一下,所以稍後可能會再次合併您的反饋。再次感謝! – vikram17000

+0

刪除兩個視圖爲我工作!謝謝! – green0range

2

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

[self.navigationController popToRootViewControllerAnimated:YES]; 
+0

非常感謝@Flexicoder。怕A不是根視圖控制器 - 事實上,根據我對湯姆的評論,我從B啓動了一個新的導航堆棧,彈出到根視圖控制器只會讓我從C到B!再次感謝! – vikram17000

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> 
+0

Thanks @BangOperator - 讓我檢查一下... – vikram17000

相關問題