如何返回初始viewcontroller

时间:2017-08-06 09:44:13

标签: ios objective-c segue uistoryboardsegue unwind-segue

我有两个视图控制器,即" ViewController"和" ProcessingViewController"。 "的ViewController"有两个文本字段和一个按钮,而" ProcessingViewController"有一个文本域。

当" ViewController"中的按钮时按下,将在" prepareForSegue"中触发一个segue。开始" ProcessingViewController"。在" ProcessingViewController"中,将完成一些处理,完成后我应该回到" ViewController"。

问题是,我该如何从ProcesingViewController返回ViewController?

注意

我在storyBoard中将ViewController中的segue添加到ProcessingViewController,并在ViewControoler中覆盖prepareForeSegue方法

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.

 NSLog(@"ProcessingViewController->before processing data: %@", 
 self.data);

self.data = @(self.data.integerValue * 10);

NSLog(@"ProcessingViewController->after processing data: %@", 
self.data);
[_textFieldValueToBeProcessed setText:[self.data stringValue]];
[self.delegate processFinished:self.data];

//[self.navigationController popViewControllerAnimated:YES];

[self dismissViewControllerAnimated:YES completion:nil];
}

故事板

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
    <adaptation id="fullscreen"/>
</device>
<dependencies>
    <deployment identifier="iOS"/>
    <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
    <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
    <!--View Controller-->
    <scene sceneID="tne-QT-ifu">
        <objects>
            <viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                    <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                    <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EgF-YL-wkx">
                            <rect key="frame" x="123" y="386" width="129" height="90"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <state key="normal" title="send"/>
                            <connections>
                                <segue destination="Z1r-zV-tYu" kind="show" identifier="segueTransitToProcessingViewController" id="8OJ-jg-a4A"/>
                            </connections>
                        </button>
                        <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="value to send" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="NGQ-pY-0eN">
                            <rect key="frame" x="48" y="202" width="279" height="30"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <nil key="textColor"/>
                            <fontDescription key="fontDescription" type="system" pointSize="14"/>
                            <textInputTraits key="textInputTraits"/>
                        </textField>
                        <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="value received" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="G88-eJ-Q35">
                            <rect key="frame" x="48" y="276" width="279" height="30"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <nil key="textColor"/>
                            <fontDescription key="fontDescription" type="system" pointSize="14"/>
                            <textInputTraits key="textInputTraits"/>
                        </textField>
                    </subviews>
                    <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                </view>
                <connections>
                    <outlet property="btnStartTransition" destination="EgF-YL-wkx" id="e07-ay-ZGi"/>
                    <outlet property="textFieldValueToBeProcessed" destination="NGQ-pY-0eN" id="RKE-Ag-AbW"/>
                    <outlet property="textFieldVlueAfterProcessing" destination="G88-eJ-Q35" id="Ih5-ih-I6p"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="116" y="123.68815592203899"/>
    </scene>
    <!--Processing View Controller-->
    <scene sceneID="Txs-Cf-DUX">
        <objects>
            <viewController id="Z1r-zV-tYu" customClass="ProcessingViewController" sceneMemberID="viewController">
                <layoutGuides>
                    <viewControllerLayoutGuide type="top" id="SNI-e8-8lZ"/>
                    <viewControllerLayoutGuide type="bottom" id="De6-we-dka"/>
                </layoutGuides>
                <view key="view" contentMode="scaleToFill" id="DME-Ab-bxq">
                    <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    <subviews>
                        <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="received value after processing" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="bcF-KR-LLP">
                            <rect key="frame" x="50" y="318" width="238" height="30"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <nil key="textColor"/>
                            <fontDescription key="fontDescription" type="system" pointSize="14"/>
                            <textInputTraits key="textInputTraits"/>
                        </textField>
                    </subviews>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                </view>
                <connections>
                    <outlet property="textFieldValueToBeProcessed" destination="bcF-KR-LLP" id="fgE-IT-PWg"/>
                </connections>
            </viewController>
            <placeholder placeholderIdentifier="IBFirstResponder" id="TXj-sv-7ZF" userLabel="First Responder" sceneMemberID="firstResponder"/>
        </objects>
        <point key="canvasLocation" x="860" y="123.68815592203899"/>
    </scene>
</scenes>

3 个答案:

答案 0 :(得分:0)

  

如果您使用过Modal Segue

self.dismiss(animated: true, completion: nil) //Swift

[self dismissViewControllerAnimated:YES completion:nil]; //Obj-C
  

如果您使用过Navigation Segue

self.navigationController?.popViewController(animated: true) //Swift

[self.navigationController popViewControllerAnimated: YES]; //Obj-C

//To pop all the view controllers between the last and root.

self.navigationController!.popToRootViewController(animated: true) //Swift

[self.navigationController popToRootViewControllerAnimated: YES]; //Obj-C

要了解更多信息,Pop the current view using Segues/Storyboard on iOS 5

答案 1 :(得分:0)

如果您的堆栈仅限于这两个控制器,那么当您按下“ViewController”中的按钮时,您可以使用segue 以模态方式呈现,并在“ProcessingViewController”中完成处理时,你可以使用 dismissViewControllerAnimated:completion 来解雇它:。

但是,如果您打算使用 Show / Push segue ,则需要在UINavigationViewController中嵌入初始视图控制器“ViewController”,然后在“ProcessingViewController”中完成处理后,您可以使用 popViewControllerAnimated:

将其实例弹出到初始视图控制器

修改 @ user2121 - 您首先需要在故事板中嵌入初始的ViewController“ViewController”并将其嵌入到UINavigationController中。 **转到故事板 - &gt;选择ViewController - &gt;编辑菜单 - &gt;嵌入 - &gt; NavigationController **

然后在“ProcessingViewController”中使用[self.navigationController popViewControllerAnimated:YES]

答案 2 :(得分:0)

我建议使用unwind segue。

来自之前的thread

&#34;步骤1)

转到&#34;上一页&#34;的代码查看您想要展开的控制器并添加它:

目标-C

- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)segue {
    //nothing goes here, unless you want to perform some other actions on unwind
}

请务必在Obj-C

中的.h文件中声明此方法

步骤2)

在故事板中,转到您的&#34;当前&#34;视图控制器(也就是你要放松的视图),只需从你的按钮拖动一个segue或者直到小橙色&#34; EXIT&#34;源视图右上角的图标。

enter image description here

现在应该可以选择连接到&#34; - unwindToThisViewController&#34;

那就是,点击你的按钮,你的segue就会放松。&#34;