-1

I am new to iOS development and having a fundamental question as how i can re-use same view controller from two different view controllers to perform a specific function.

Here is my problem. Say we have three view controllers A, B and C. And C performs a specific function and it's output i want to use in both A and B.

Say A, has two edit box A1 and A2 and a button "Call C", A1 is edit box that user fills in and to take input for A2, we have to call C by pressing "Call C" button that i have segued in storyboard.

Similarly B, has two edit box B1 and B2 and also a button "Call C". B1 is edit box that user fills in and to take input for B2, we have to call C by pressing "Call C" button that i have segued in storyboard.

Essentially i want to make use of C for two different view controllers where C will return some string to calling view that i can fill in B2 or C2 edit boxes.

Here are two ways to unwind:

Method - A:

Programmatically you can call performSegueWithIdentifier to caller view controller and return to it, but in this process called view controller (i.e. A or B) gets re-created, so input that may have been done by user on control A1 or C1 is lost as A and B views are re-created.

So i can programmatically unwind but state is lost of calling view controller as it gets re-created.

Method - B:

In both A and B i create unwind methods like:

-(IBAction) receiveDataFromCtoA:(UIStoryboardSegue *)segue

-(IBAction) receiveDataFromCtoB:(UIStoryboardSegue *)segue
respectively.

But in view C, i can only unwind in Exit to either of the two functions however it does allow me to retain value of calling view controller controls A1 / C1.

So unwinding from Exit works as expected, but i can only unwind to either of the two and thus i can't reuse view controller C for both.

So whole point is how you can make use of a view controller which takes some input, performs some calculation and return results and can be used by any calling view.

I did look at following post:

Unwind segues for 2 View Controllers

But it does not answer.

Any help is appreciated.

Community
  • 1
  • 1
Upal
  • 11
  • 2
  • tldr; You need to show a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). Help yourself get an answer. – StefanS Mar 10 '16 at 11:44
  • Really confusing description, But as i have understand is that you are doing calculation in Class A and Class B and want to display its result it Class C, Is that Correct? and second thing i can think of according to your Description is You want to add some text in Class C and pass that value back to Class A and Class B? Which one of the scenario you are asking? – Dilip Manek Mar 10 '16 at 11:47
  • Hi Dilip, I want to "add some text in Class C and pass that value back to Class A and Class B". Thanks – Upal Mar 11 '16 at 11:42

1 Answers1

0

You should be using delegates in order to achieve this.

Delegates and Data Sources

I would suggest looking here for more info

How do I create delegates in Objective-C?

Community
  • 1
  • 1
themobileapper
  • 184
  • 1
  • 4