[英]How to pass data between UIViewControllers without presenting it
I would like to move data between two differents UIViewControllers, without segue nor pushing one. 我想在两个不同的UIViewControllers之间移动数据,而无需选择或推送一个。 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
我尝试使用协议/委托,但未能正确定义委托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: 对于您的情况,并且您不喜欢全局变量,如果两个控制器都存在,只是不能存在,我认为有一些方法可以做到:
if one or more controller is not exist, the 2) must be ok. 如果不存在一个或多个控制器,则2)必须正常。
Based on the comments, I created a singleton class that inherit from my data class (in Swift) : 根据评论,我创建了一个单例类,该类从我的数据类继承(在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. 您可以传递Dictionary
,也可以传递动作。 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 如果您需要更多说明,可以在这里看看: 在视图控制器之间传递数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.