-1

I would like to move data between two differents UIViewControllers, without segue nor pushing one. There are some questions about this subject, but I didn't find any clear answer.

I tried using protocol / delegate, but I failed to correctly define the delegate vc1.delegate = vc2

Do I have to define a global variable ? Do you have any other idea ?

Thank you.

Nahouto
  • 1,375
  • 2
  • 18
  • 31
  • Yes. You can define in global variable in AppDelegate. – Mitul Marsoniya Feb 22 '16 at 08:47
  • I do not like global variables. But it seems I have no choice. – Nahouto Feb 22 '16 at 08:49
  • Use "NSUserDefault" for store any data(value) and get any where in application. – Mitul Marsoniya Feb 22 '16 at 08:52
  • You can store your data external (configuration, file, db) and access it from your second view controller or use a global variable. I don't think there's another solution than that. – Thomas Feb 22 '16 at 08:52
  • Base on your requirement if you have more data than you can use (configuration, file, db) or if you have small data like login current user info then use NSUserDefault. – Mitul Marsoniya Feb 22 '16 at 08:54
  • You are right, I will create a singleton object. – Nahouto Feb 22 '16 at 08:56
  • I think you can use Notification to send data without presenting view controller.Add notification observer to view controller and post an notification from other view controller . . .But if you just need to save object and reuse it any time of instance then use singleton – Anil solanki Feb 22 '16 at 09:06

4 Answers4

1

you can create a singleton and store the object that you want to share :

Singleton.h

@interface Singleton : NSObject

@property (nonatomic, strong) YourObjectClass *yourObject;

+ (Singleton *)sharedInstance;
+ (YourObjectClass *)getYourObject;
+ (void)setYourObject:(YourObjectClass *)yourObject;

@end

Singleton.m

#import "Singleton.h"
#import "YourObjectClass.h"

@implementation Singleton

static Singleton *sharedObject;

+ (Singleton *)sharedInstance;
{
    if (sharedObject == nil) {
        static dispatch_once_t pred;
        dispatch_once(&pred, ^{
            sharedObject = [[Singleton alloc] init];
        });
    }
    return sharedObject;
}

+ (YourObjectClass *)getYourObject
{
    Singleton *singleton = [Singleton sharedInstance];
    return singleton.yourObject;
}

+ (void)setYourObject:(YourObjectClass *)yourObject
{
    Singleton *singleton = [Singleton sharedInstance];
    singleton.yourObject = yourObject;
}
poyo fever.
  • 742
  • 1
  • 5
  • 22
1

For general situation, one shared memory will be omnipotent, so a singleton is a custom way.

For your situation, and you don't like global variable, if the two controllers are exist, just can not present, I think there are some ways to do it:

  1. Post notification
  2. NSUserDefaults/DB/File
  3. Delegate/Block

if one or more controller is not exist, the 2) must be ok.

Blues Zhou
  • 306
  • 2
  • 16
0

Based on the comments, I created a singleton class that inherit from my data class (in Swift) :

import Foundation

class CurrentMyObject: MyObject {

// get the current CurrentMyObject
class var sharedInstance: CurrentMyObject {
    struct Static {
        static var instance: CurrentMyObject?
        static var token: dispatch_once_t = 0
    }
    dispatch_once(&Static.token) {
        Static.instance = CurrentMyObject()
    }
    return Static.instance!
}
}
Nahouto
  • 1,375
  • 2
  • 18
  • 31
0

What do you mean with "pass data"? You can pass a Dictionary and you can pass an action. The new controller is just a class, so this means that you can alloc the object and pass it what do you want.

class ViewController1: UIViewController
{
    weak newController : ViewController2?

    override func viewDidLoad() {
        super.viewDidLoad()

        newController = ViewController2()
        newController.dataReceived = [:]
    }

    func presentNewController()
    {
        // Do whatever with `newController`
    }
}

class ViewController2: UIViewController {
    var dataReceived : AnyObject?
}

If you need some more explanations, you can take a look here: Passing Data between View Controllers

Community
  • 1
  • 1
Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
  • This does not seem to work, because ViewController2 instantiate a new object when appearing. Besides, I already read this link, but it deals only with "contiguous" viewControllers by using segue or pushing it. – Nahouto Feb 22 '16 at 09:50
  • You said without pushing or presenting the new one. If you need to present it later, you can store the controller instance in a property and then use the same one to present – Luca D'Alberti Feb 22 '16 at 09:51
  • hum, I understand... But I need to pass this viewcontroller as a property in all the viewcontrollers it will transition by, and eventually use it to present. – Nahouto Feb 22 '16 at 09:55
  • Why do you want to do that? That's not so correct. Doing this you will have a lot of problem because everyone can edit that instance! – Luca D'Alberti Feb 22 '16 at 09:57
  • You are right. Here is my storyboard : VC1 -> VC2 -> VC3 -> VC4 -> VC5. And I need to pass an object from VC1 to VC5. The simplest way seems to create a Singleton object. – Nahouto Feb 22 '16 at 10:00
  • Normally I use a `RootViewController` that is the responsible for this kind of stuff. Everyone as an instance to it (it hasn't public properties, so no one can modify it) and they can access just to some methods to configure this kind of scene – Luca D'Alberti Feb 22 '16 at 10:03
  • Thanks to your idea, your way seems safer – Nahouto Feb 22 '16 at 10:07