0

I have 2 ViewControllers for the purpose of Landscape Orientation

My First VC (portraitVC) has multiple UITextFields (about 30 of them) I'm using NSNotifications to observe when the interface orientation changes and when it does it pushes a viewController on Screen (specific for landscape)

My question now is, is it possible to share My UITexFields amongst both VC's like if i enter something in portrait then when I go into landscape mode I don't lose the Text I entered on the TextField: and it should work the other way around too

You're help or guide is very appreciated

Taskinul Haque
  • 724
  • 2
  • 16
  • 36
  • 1
    Here's a good guide (with source): http://iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-and-other-objects-link-fixed.html – Evan Stoddard Sep 03 '13 at 18:28

1 Answers1

1

I can think of a couple ways to handle this.

First, reconsider your choice to use two different VCs to handle orientation changes. How hard would it be to update the UI on your first VC to look appropriate in portrait?

Second, abstract the data out into a struct or a class so that you can pass it back and forth between your two VCs

@interface MyDataStorage : NSObject {
    NSString* text1;
    NSString* text2;
    NSString* text3;
    NSString* text4;
}

Lastly, you could create a protocol to keep the two VCs in sync:

@protocol VCSync : NSObject {
- (void)updateTextBox1:(NSString*)contents;
- (void)updateTextBox2:(NSString*)contents;
...
}

You would need to keep references to the other VC so that you can call the appropriate method from your TextBox delegate callbacks.

RE: Documentation link

If you're referencing this passage: There are better ways to provide unique views for different orientations, such as presenting a new view controller then you should read the previous sentence.

A new VC is only suggested if you're swapping out your entire view hierarchy. If you are only resizing the textboxes then I would just write the code to do so in willAnimateRotationToInterfaceOrientation:duration: since then you won't have to move any of the information from one VC to another.

If you still decide to go ahead with another VC then I would create a class which just contains a bunch of NSStrings or NSAttributedStrings as your needs dictate. You would then need a method on each of your VCs which goes something like:

- (void)updateTextboxesWithData:(MyDataStorage*)data {
    for(UITextbox in self.textboxes) {
        t.text = data.string;
    }
}
Clay
  • 319
  • 2
  • 15
  • Thank you for your quick response Clay - well I want LAndscape to look different entirely - also according to Apple documentation, https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html - they recommend using 2 separate VC's - what other ways could you think of doing this ? - please do share – Taskinul Haque Sep 03 '13 at 18:55