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

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

问题描述

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



我的第一个视图控制器(A)中包含所有对象我的存储的应用程序数据(请忽略NSUserDefaults为此),当我按下一个按钮时,需要由第二个视图控制器(B)访问。

解决方案

您有一个选项是将您的日期模型声明为您的实例变量应用程序委托(如其他评论者所提到的)。



而不是根据nevan的建议引用应用程序委托,替代方法是将属性添加到视图控制器类(A和说你想在你的视图控制器之间共享一个数据模型对象,你可以添加一个属性到每个:

  @interface AViewController:UIViewController {
MyDataModel * model;
}

@property(nonatomic,retain)MyDataModel * model;

@end

@interface BViewController:UIViewController {
MyDataModel * model;
}

@property(nonatomic,retain)MyDataModel * model;

@end

当您初始化视图控制器时,您可以设置此属性到对象上下文已被初始化。



您已经提到了一个标签栏控制器。如果您的视图控制器通过IB连接,则必须在显示标签栏控制器之前在应用程序委托 applicationDidFinishLaunching:方法中设置这些参数:

  @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 *)应用程序
{
...

aViewController.model = model;

bViewController.model = model;

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

不要忘记在视图控制器的 dealloc 方法。






另一种选择是使用单例对象。一个简单的单例示例:

  @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

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

  MyDataModel * model = [MyDataModel sharedDataModel]; 

另请参见这个堆栈溢出关于单身人士的讨论。


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

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).

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.

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

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

See also this stack overflow discussion about singletons.

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

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