Question

I'm building a multi view app for iPad. For now, I have 3 ViewControllers on a SplitViewController Application. How do I send data that was inserted on ViewController 1 and View Controller 2 to ViewController 3? Here is an image from my storyboard.

Storyboard

Was it helpful?

Solution 3

Create new class Singleton with parent NSObject

@interface Singleton : NSObject

@property (nonatomic, strong) NSString *stringParams1;
@property (nonatomic, strong) NSString *stringParams1;

+ (Singleton *)sharedInstance;

@end

Singleton.m:

@implementation STGameHelper

+ (Singleton *)sharedInstance
{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

@end

So in the view controller 1 and 2 just access this singleton:

Singleton *s = [Singleton sharedInstance];

s.stringParams1 = textField1.text;
s.stringParams2 = textField12.text;

In the view controller 3 you will have the same instance of singleton. Access it in viewDidLoad for example:

Singleton *s = [Singleton sharedInstance];

textField1On3rdViewcontroller.text = s.stringParams1;
textField2On3rdViewcontroller.text = s.stringParams2;

Read more about singleton here: link

As you are beginner I recommend you using singleton instead of core data right now, but if you need to store the data after they were entered I will suggest you read some tutorial how to work with core data because it is very powerful thing and it will make you life easiest. But for now it's enough to have singleton if you need to store data after app will be closed for example.

OTHER TIPS

Consider you want to send string data to ViewController2,ViewController3 from ViewController1.

Make property of the string variable in ViewController2 and ViewController3.

@property(nonatomic,strong) NSString *str;

And while pushing the ViewController2 and ViewController3:

  ViewController2 *viewController = [ViewController2 alloc]init];
  viewController2.str = @"Some text";
  [self.navigationController pushViewController:viewController2 animated:YES];

And you have the data send from ViewController1 in ViewController2.

You have to keep a reference to the data and either pass it between view controllers, or retrieve it from somewhere. It can be from a local database, from an in-memory object in a singleton instance or whatever.

Maybe in your use case, the view controller that triggers the segues (the one with a lot of buttons) can hold an object containing the data, and pass it to the view controllers 1, 2 and 3 when the are about to appear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top