0

i have two view controller namely, "ViewController" and "ProcessingViewController". "ViewController" has two textfields and a button, while "ProcessingViewController" has a textfield.

when the button in "ViewController" is pressed, a segue will be triggered in "prepareForSegue" to start "ProcessingViewController". in "ProcessingViewController", some processing will be done and when finished I should go back to "ViewController".

the question is, how should I go back to ViewController from ProcesingViewController?

note:

I added a segue from ViewController to ProcessingViewController in the storyBoard, and I override prepareForeSegue method in ViewControoler

code:

- (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];
}

storyboard:

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

Amrmsmb
  • 1
  • 27
  • 104
  • 226

3 Answers3

0

If you have used Modal Segue

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

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

If you have used 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

To understand more, Pop the current view using Segues/Storyboard on iOS 5

Hemang
  • 26,840
  • 19
  • 119
  • 186
0

If in your stack is limited to these two controllers, then when you press a button in "ViewController", you could present modally using a segue and when you are done with processing in "ProcessingViewController", you can dismiss it using dismissViewControllerAnimated:completion:.

If however, you plan to use a Show/Push segue, you will need to embed your initial view controller "ViewController" in a UINavigationViewController and then when you are done processing in "ProcessingViewController", you can use an instance of that to pop to the initial view controller using popViewControllerAnimated:

EDIT @user2121 - You will first need to embed your initial ViewController "ViewController" in your storyboard and embed it in a UINavigationController. **Go to Storyboard -> Select your ViewController -> Editor Menu -> Embed In -> NavigationController **

Then in "ProcessingViewController" use [self.navigationController popViewControllerAnimated: YES]

  • i change the segue type to present modally, and I used your code but it is not working..would you please have a look at the code above – Amrmsmb Aug 07 '17 at 11:37
  • You are not required to use a navigation controller. The best way would be to use an unwind segue. See my answer below – FightOn Aug 07 '17 at 19:44
  • Did you call **dismissViewControllerAnimated:completion** after all processing in the "ProcessingViewController" was finished?//////////////////////////////////////////////////////////////////////////////////////// self.dismiss(animated: true, completion: nil) //Swift/////////////////////////////////////‌​////////////////////‌​/////////////////////////////////// [self dismissViewControllerAnimated:YES completion:nil]; //Obj-C – Ankit Batra Aug 08 '17 at 13:37
0

I would recommend using an unwind segue.

From a previous thread :

"Step 1)

Go to the code for the "Previous" view controller that you wish to unwind to and add this:

Objective-C

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

Be sure to also declare this method in your .h file in Obj-C

Step 2)

In storyboard, go to your "Current" view controller (aka, the view that you want to unwind from) and simply drag a segue from your button or whatever up to the little orange "EXIT" icon at the top right of your source view.

enter image description here

There should now be an option to connect to "- unwindToThisViewController"

That's it, your segue will unwind when your button is tapped."

FightOn
  • 40
  • 1
  • 11
  • I did as you explained in your ab´answer, but now the problem is when I ctrl+drage the button to the EXIT icon, the EXIT icon is never highlighted and consequently I do not get the menu that display the method of the unwind segue "-unwindToThisViewController"..please le t em know how to solve this issue – Amrmsmb Aug 08 '17 at 09:25