Ios 如何创建一个类的单个实例并在多个视图中更新该类的数据?
我正在创建一个iPad应用程序,用于对员工进行调查。此应用程序将包含多个UIViewController 到目前为止我所做的:Ios 如何创建一个类的单个实例并在多个视图中更新该类的数据?,ios,objective-c,iphone,uiviewcontroller,Ios,Objective C,Iphone,Uiviewcontroller,我正在创建一个iPad应用程序,用于对员工进行调查。此应用程序将包含多个UIViewController 到目前为止我所做的: 我创建了一个“调查”类。此类包含我将在调查过程中填写的字段的属性。姓名、电子邮件地址、年龄等基本信息 我为UIViewController创建了一个名为SVViewController的子类。在该类中,我在导入Survey类后创建并初始化了一个名为Survey的Survey实例 我访问了我已经创建并与故事板视图关联的每个UIViewController类,并更改了它们的
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.h
#进口
@接口调查:NSObject
@属性(非原子,强)NSMutableString*名称;
@属性(非原子,强)NSMutableString*电子邮件地址;
@结束
调查
#导入“Survey.h”
@实施情况调查
@结束
SVViewController.h
#进口
#导入“Survey.h”
@界面SVViewController:UIViewController{
调查*调查;
}
@财产(非原子、保留)调查*调查;
+(id)共享管理者;
@结束
SVViewController.m
#导入“SVViewController.h”
@界面SVViewController()
@结束
@SVViewController的实现
@综合调查;
+(id)共享管理器{
静态SVViewController*sharedSVViewController=nil;
@同步(自){
if(sharedSVViewController==nil)
sharedSVViewController=[[self alloc]init];
}
返回共享VVIEWCONTROLLER;
}
-(id)init{
if(self=[super init]){
调查=[[survey alloc]init];
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后执行任何其他设置。
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
我敢肯定这是一种混乱,我试着玩弄了很长一段时间。我认为正在发生的是创建了一个Survey实例,但当我尝试为Survey属性(名称、电子邮件地址等)设置值,然后重写它们以检查它们是否更改时,我只会返回null。我假设这是因为我没有在任何地方初始化属性,但是我在想在哪里初始化时遇到了很多麻烦
先谢谢你 您想创建一个
调查
的实例,但看起来您已经编写了创建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
您想创建一个
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
要在应用程序内共享数据,您可能需要使用singleton。例如,在.h中创建名为CommonData的.h和.m
#import <Foundation/Foundation.h>
@class User; // for your case, you may want to use Survey
@interface CommonData : NSObject
@property (strong) User *currentUser; // this is to store data used across all UIViewController
+ (CommonData *)sharedModel;
@end
无论何时需要设置或获取,都可以通过[CommonData sharedModel].currentUser.xxx访问数据。我曾经使用AppDelegate来存储应用程序中共享的数据。然而,我遇到了一些奇怪的问题(请参阅)。到目前为止,使用Singtelon还不错。希望能有帮助
问候
Hammer要在应用程序内共享数据,您可能需要使用singleton。例如,在.h中创建名为CommonData的.h和.m
#import <Foundation/Foundation.h>
@class User; // for your case, you may want to use Survey
@interface CommonData : NSObject
@property (strong) User *currentUser; // this is to store data used across all UIViewController
+ (CommonData *)sharedModel;
@end
无论何时需要设置或获取,都可以通过[CommonData sharedModel].currentUser.xxx访问数据。我曾经使用AppDelegate来存储应用程序中共享的数据。然而,我遇到了一些奇怪的问题(请参阅)。到目前为止,使用Singtelon还不错。希望能有帮助
问候
Hammer好的,到目前为止我同意你的看法,但是如果我想让其他UIViewController看到Survey实例,我必须在父类(SVViewController)中包含Survey实例,对吗?我看到您的代码块列出了_instance=[[Survey alloc]init],它似乎可以做到这一点,但我如何包括该Survey实例,使其可见?创建UIViewController的新子类并更改继承是否犯了错误?选择器
[Survey sharedInstance]
可以在任何类中访问。这是一个在Survey
上的静态方法,它为您提供了singleton实例。非常感谢您的帮助。好的,到目前为止我同意您的看法,但是如果我希望Survey实例对我的其他UIViewController可见,我必须在父类(SVViewController)中包含Survey实例,对吗?我看到您的代码块列出了_instance=[[Survey alloc]init],它似乎可以做到这一点,但我如何包括该Survey实例,使其可见?创建UIViewController的新子类并更改继承是否犯了错误?选择器[Survey sharedInstance]
可以在任何类中访问。这是一个在Survey
上的静态方法,它为您提供了singleton实例。非常感谢你的帮助。