user1483652
user1483652

Reputation: 799

How to link outlets across view controllers?

im creating an app and currently has 6 view controllers, now in view controller 6 I have a text field, and when they edit it, it needs to edit a label in view controller 3, How can i link an outlet declared in viewcontroller6.h in storyboard to something in view controller 3? I know its complicate but any help will be really great! :) thanks

Upvotes: 3

Views: 282

Answers (3)

Dustin
Dustin

Reputation: 6803

I've answered similar questions to this before:

Example: Delegates.

The quick answer is that you can't really "link" an outlet in vc6 to something in vc3 like you would with a control-drag in IB. The best way to accomplish what you want is with the delegate pattern, which I outline in the linked question.

It takes a bit of extra work, but it's a very important skill to have.

Upvotes: 0

Rick
Rick

Reputation: 1818

You cannot link an outlet from vc6 to vc3 in storyboard. There are a number of ways to pass data from vc6 to vc3:

  1. Use delegate pattern (as Dustin mentioned) to pass the textfield data from vc6 to vc3. However, if your view controllers are chained in sequence from vc3 to vc4 to vc5 to vc6, using this approach can be tedious.

  2. Use shared instance (singletons). I use the code from here. I would recommend this approach if you have quite a number of data to pass around.

  3. Use NSUserDefault to remember the textfield data and when displaying the label in vc3, read back the values using [NSUserDefaults standardUserDefaults]. Good if you have only very few data to pass around. Another advantage of this method is you can remember this data for next launch and the label in vc3 can be displayed correctly before textfield in vc6 is shown to the user for input.

Upvotes: 1

If Pollavith
If Pollavith

Reputation: 228

First of all, in your viewcontroller6, you need to #import "viewcontroller3.h". Then in viewcontroller6.m, you can create an instance of viewcontroller3. You then have access to all of viewcontroller3's data. You can set the data in that instance of viewcontroller3 from viewcontroller6.m and push to viewcontroller3's views, and your data should be there.

Although if its a label, you may need to create a property of NSString* stringText in your viewcontroller3.h, and synthesize it in your viewcontroller3.m file, then in view controller6, set that string to the outlet value declared in viewcontroller6. And after you've pushed it to view controller 3. do something like label.text = stringText in the viewdidload() of view controller3;

So now, your label in viewcontroller3 should be updated to whatever was in stringText.. which is set from viewcontroller6.

Upvotes: 1

Related Questions