简体   繁体   中英

How to pass data between UIViewControllers without presenting it

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.

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;
}

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.

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!
}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM