如何在 iPhone 上的 UIViewController 之间共享对象? [英] How do I share an object between UIViewControllers on iPhone?

查看:20
本文介绍了如何在 iPhone 上的 UIViewController 之间共享对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个标签栏应用程序,每个标签都有一个单独的视图控制器.

My application is a tab bar application, with a separate view controller for each tab.

我的第一个视图控制器 (A) 中有一个对象,其中包含我存储的所有应用程序数据(请忽略 NSUserDefaults),当我按下按钮时,第二个视图控制器 (B) 需要访问该对象.我怎样才能以最好的方式实现这一目标?

I have an object in my first view controller (A) which contains all my stored application data (Please ignore NSUserDefaults for this) which needs to be accessed by the second view controller (B) when I press a button on it. How can I achieve this in the best way?

推荐答案

您可以选择将日期模型声明为应用委托的实例变量(正如其他评论者所提到的).

One option you have is to declare your date model as instance variables of your app delegate (as mentioned by other commenters).

替代 nevan 建议的引用应用委托的替代方法是为您的数据模型添加一个属性到您的视图控制器类(A 和 B).

Instead of referencing the app delegate as suggested by nevan an alternative is to add a property to your view controller classes (A and B) for your data model.

假设您想在视图控制器之间共享一个数据模型对象,您可以为每个视图控制器添加一个属性:

Say you wanted to share a data model object between your view controllers you can add a property to each:

@interface AViewController : UIViewController {
    MyDataModel *model;
}

@property (nonatomic, retain) MyDataModel *model;

@end

@interface BViewController : UIViewController {
    MyDataModel *model;
}

@property (nonatomic, retain) MyDataModel *model;

@end

当您初始化视图控制器时,您可以将此属性设置为之前初始化的对象上下文.

When you initialise your view controller you can then set this property to the object context initialised previously.

您提到了一个标签栏控制器.如果您的视图控制器通过 IB 连接,您只需在应用程序委托 applicationDidFinishLaunching: 方法中设置这些参数,然后显示标签栏控制器:

You have mentioned a tab bar controller. If your view controllers are wired through IB all you have to do is to set these parameters in your application delegate applicationDidFinishLaunching: method, before the tab bar controller is displayed:

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{

    MyDataModel *model;
    AViewController *aViewController;
    BViewController *bViewController;
    ...
}

@property (retain) IBOutlet AViewController *aViewController;
@property (retain) IBOutlet BViewController *aViewController;

@end

@implementation MyAppDelegate

...

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
...

    aViewController.model = model;

    bViewController.model = model;

    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
}

不要忘记在视图控制器的 dealloc 方法中释放模型.

Don't forget to release the model in your view controller's dealloc method.

另一种方法是使用单例对象.一个简单的单例示例:

The alternative is to use a singleton object. An simple singleton example:

@interface MyDataModel : NSObject
{
}

+ (MyDataModel *) sharedDataModel;

@end

@implementation MyDataModel

static MyDataModel *sharedDataModel = nil;

+ (MyDataModel *) sharedDataModel
{

    @synchronized(self)
    {
        if (sharedDataModel == nil)
        {
            sharedDataModel = [[MyDataModel alloc] init];
        }
    }
    return sharedDataModel;
}

@end

您可以使用类似于以下内容的所有视图控制器访问此数据模型:

You can access this data model from all your view controllers with something similar to the following:

MyDataModel *model = [MyDataModel sharedDataModel];

另请参阅 this 关于单例的堆栈溢出讨论.

See also this stack overflow discussion about singletons.

这篇关于如何在 iPhone 上的 UIViewController 之间共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆