【问题标题】:How to go back to the initial viewcontroller如何回到初始视图控制器
【发布时间】:2018-01-13 19:15:22
【问题描述】:

我有两个视图控制器,即“ViewController”和“ProcessingViewController”。 “ViewController”有两个文本域和一个按钮,而“ProcessingViewController”有一个文本域。

当“ViewController”中的按钮被按下时,“prepareForSegue”中会触发一个segue来启动“ProcessingViewController”。在“ProcessingViewController”中,将完成一些处理,完成后我应该返回“ViewController”。

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

注意

我在storyBoard中添加了一个从ViewController到ProcessingViewController的segue,并覆盖了ViewController中的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>

【问题讨论】:

  • 你需要一个 segue 回到你的 ViewController
  • 你能添加你的故事板场景吗
  • 您是否将 ViewController 嵌入到导航控制器中,您将如何从 ViewController 导航到 ProcessingViewController ,您能否添加一些与此相关的代码或其他 UI 设计
  • @Anbu.Karthik 我添加了故事板
  • @user2121 - 如果您按下完成按钮,则将方法调用为[self dismissViewControllerAnimated:YES completion:nil]; 以返回视图控制器

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


【解决方案1】:

如果你使用过模态转场

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

【讨论】:

  • 实际上,我尝试了您提供的两种解决方案,但第二个 VC 并没有回到第一个
  • 最简单的方法是展开转场。看我的回答
【解决方案2】:

如果您的堆栈中仅限于这两个控制器,那么当您按下“ViewController”中的按钮时,您可以模态显示使用 segue 并在“ProcessingViewController”中完成处理,您可以使用 dismissViewControllerAnimated:completion:.

将其关闭

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

弹出到初始视图控制器

编辑 @user2121 - 您首先需要将初始 ViewController“ViewController”嵌入到故事板中,并将其嵌入到 UINavigationController 中。 **转到 Storyboard -> 选择您的 ViewController -> Editor Menu -> Embed In -> NavigationController **

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

【讨论】:

  • 我将 segue 类型更改为模态呈现,我使用了您的代码,但它不起作用..请您看看上面的代码
  • 您不需要使用导航控制器。最好的方法是使用 unwind segue。请参阅下面的答案
  • 在“ProcessingViewController”中的所有处理完成后,您是否调用了 dismissViewControllerAnimated:completion?/////////////// ///////////////////////////////////////// ///////////////// self.dismiss(animated: true, completion: nil) //Swift///////////// ///////////////////‌​//////////////////‌​//// ///////////////////////////[selfdismissViewControllerAnimated:YES 完成:nil]; //Obj-C
【解决方案3】:

我建议使用 unwind segue。

来自以前的thread

“第 1 步)

转到您希望展开的“上一个”视图控制器的代码并添加:

目标-C

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

请务必在 Obj-C 中的 .h 文件中声明此方法

第 2 步)

在情节提要中,转到您的“当前”视图控制器(也就是您要从中放松的视图),然后只需将一个 segue 从您的按钮或其他任何东西拖到右上角的橙色小“退出”图标您的源视图。

现在应该有一个连接到“- unwindToThisViewController”的选项

就是这样,当你的按钮被点击时,你的 segue 会展开。"

【讨论】:

  • 我按照您在 ab´answer 中的解释做了,但现在的问题是当我按 ctrl+拖动按钮到 EXIT 图标时,EXIT 图标永远不会突出显示,因此我没有得到菜单显示展开segue“-unwindToThisViewController”的方法..请让他们知道如何解决这个问题
猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多