ios - 如何创建类的单个实例并在多个 View 中更新该类的数据?

标签 ios objective-c iphone uiviewcontroller

我正在创建一个 iPad 应用程序,我将使用它在员工中进行调查。这个应用程序将包含多个 UIViewControllers。

到目前为止我做了什么:

  1. 我创建了一个“调查”类。此类包含我将在调查过程中填写的字段的属性。姓名、电子邮件地址、年龄等基本信息。

  2. 我为 UIViewController 创建了一个名为 SVViewController 的子类。在该类中,我在导入 Survey 类后创建并初始化了一个名为 survey 的 Survey 实例。

  3. 我转到我已经创建并与 Storyboard View 相关联的每个 UIViewController 类,并更改了它们的继承,以便它们从 SVViewController 继承。

我对这一切还很陌生,我不知道该如何继续。我希望 Survey 实例(再次称为“调查”)包含用户填写时的所有数据。当有人在第一个 View 中输入他们的名字时,我希望它与该调查实例进行通信以更新姓名。然后我想进入下一个 View 并从调查实例中提取“名称”的值。

基本上,我需要调查,调查的这个实例,以在所有 View 中保持不变。我在网上找到了一个关于单例模式的例子,然后我就从它开始,但是我遇到了一些麻烦。在继续之前,我将粘贴一些代码:

Survey.h
    #import <Foundation/Foundation.h>
    @interface Survey : NSObject
    @property (nonatomic, strong) NSMutableString *name;
    @property (nonatomic, strong) NSMutableString *emailAddress;
    @end


Survey.m
    #import "Survey.h"
    @implementation Survey
    @end

SVViewController.h
    #import <UIKit/UIKit.h>
    #import "Survey.h"
    @interface SVViewController : UIViewController {
    Survey *survey;
    }
    @property (nonatomic, retain) Survey *survey;
    +(id)sharedManager;
    @end


SVViewController.m

    #import "SVViewController.h"
    @interface SVViewController ()
    @end

    @implementation SVViewController
    @synthesize survey;
    +(id)sharedManager{
        static SVViewController *sharedSVViewController = nil;
        @synchronized(self){
            if(sharedSVViewController == nil)
                sharedSVViewController = [[self alloc]init];
        }
        return sharedSVViewController;
    }
    - (id) init{
        if(self = [super init]){
            survey = [[Survey alloc] init];
        }
        return self;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end

我敢肯定这有点乱,我试了很长时间。我认为正在发生的事情是正在创建 Survey 的单个实例,但是当我尝试为 Survey 的属性(名称、电子邮件地址等)设置值,然后重写它们以检查它们是否更改时,我只是得到空值。我假设这是因为我没有在任何地方初始化该属性,但我在弄清楚在哪里做这件事时遇到了很多麻烦。

提前致谢。

最佳答案

您想创建 Survey 的单个实例,但看起来您已经编写了代码来创建 SVViewController 的单个实例。相反,试试这个:

@interface Survey : NSObject
+ (instancetype)sharedInstance;
@property (nonatomic, strong) NSMutableString *name;
@property (nonatomic, strong) NSMutableString *emailAddress;
@end

@implementation Survey
+ (instancetype)sharedInstance {
  static Survey *_instance;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    _instance = [[Survey alloc] init];
  });
  return _instance;
}
@end

关于ios - 如何创建类的单个实例并在多个 View 中更新该类的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26632934/

相关文章:

iphone - 在堆叠 View 之间传递数据

iphone - 在 NSString 中查找子字符串的所有位置(不仅仅是第一个)

ios - 当我们使用 Storyboard 时,如何在 Objective-C 中实现自定义 init 方法?

ios - 如何在 JSONDecoder 中处理空日期字符串

ios - 是否可以从 UIWebView 获取图像并在不重新加载的情况下本地显示它?

objective-c - 自定义 UIFont 为零

iphone - ios - 将单个手势识别器添加到多个标签

ios - 几个小时后推送通知的委托(delegate)方法未命中

ios - 日历的实现

iphone - 如何使手势识别器在动画 UIImage View 中工作