我一直在寻找tableviews和singleton类,但找不到任何解决方案,所以我在这里。
当用户选择我想将所选数据发送到singleton类中的数组并将其打印到另一个viewcontroller中的屏幕时,我在viewcontroller中有一个表视图。
这是我的Singleton类代码:
#import <Foundation/Foundation.h>
@interface DataController : NSObject {
NSArray* standLoc;
}
@property (readonly)NSArray* standLoc; // stand location
+(DataController*)sharedInstance;
@end
#import "DataController.h"
@implementation DataController
@synthesize standLoc;
+(DataController*)sharedInstance
{
static DataController* sharedInstance = nil;
if (!sharedInstance)
{
sharedInstance = [[DataController alloc]init];
}
return sharedInstance;
}
@end
那么我该如何将数据传递给我试过的Singleton类中的数组
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
StartHuntViewController *startHuntController = [[StartHuntViewController alloc] initWithNibName:@"StartHuntView" bundle:nil];
DataController* sharedSingleton = [DataController sharedInstance];
sharedSingleton = [stands objectAtIndex:indexPath.row];
startHuntController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:startHuntController animated:YES];;
[startHuntController release];
startHuntController =nil;
}
在调试中我可以看到所选项目在sharedSingleton中,但我怎样才能将它传递给NSArray * standLoc?
修改
所以我已经编辑了我的代码,它现在可以正常运行多个控制器视图
我的Singleton .m和.h:
#import <Foundation/Foundation.h>
@interface DataController : NSObject {
NSString* standLoc;
}
@property (nonatomic,retain)NSString* standLoc; // stand location
+(DataController*)sharedInstance;
-(void) setData: (NSString *) data;
@end
#import "DataController.h"
@implementation DataController
static DataController* sharedInstance = nil;
@synthesize standLoc;
+(DataController*)sharedInstance
{
@synchronized (self) { //this ensure this methods will not be called at the same time..
if(sharedInstance == nil){
[[self alloc] init];
}
}
return sharedInstance;
}
+(id) allocWithZone:(NSZone *)zone{
@synchronized (self){
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
-(id)copyWithZone:(NSZone *)zone{ // incase if we want to copy our singleton instance
return self;
}
//to protect singleton from deallocation, we need to override some functions of memory allocation
-(id) retain {
return self;
}
-(id) autorelease{
return self;
}
-(NSUInteger ) retainCount{
return NSUIntegerMax;
}
-(id) init{ // lets set the default data in it
@synchronized (self){
[super init];
standLoc = [[NSString alloc] initWithString:@"Stand Loc"];//for performance, as we expect 5 digits from server, it's size was set to another 5 digits..
return self;
}
}
-(void) setData: (NSString *) data{ // this is the function to set static data which is the member of the class, reaching data will be allowed with this method
@synchronized (self){
if (standLoc != data) {
[standLoc release];
standLoc = [data retain];
}
}
}
-(NSString *) standLoc{
@synchronized(self){
return standLoc;
}
}
将数据传递给singleton:
DataController* sharedSingleton = [DataController sharedInstance];
NSString* transfer = [stands objectAtIndex:indexPath.row];
[sharedSingleton setData:transfer];
答案 0 :(得分:0)
你为什么需要单身人士?如果只是将数据直接从一个视图控制器传递到下一个视图控制器,那么您的代码将更简单,更易于维护。听起来你正在使用导航控制器,这样当表格中的单元格被点击时,表格的视图控制器可以推送一个新的细节控制器,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
detailViewController.data = [tableData objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:detailController animated:YES];
}
答案 1 :(得分:0)
您想要的语法类型(在解决上述两点之后)是:
DataController * sharedSingleton = [DataController sharedInstance];
sharedSingleton.standLoc = //单身内容应该是什么数据元素;
答案 2 :(得分:0)
为什么使用@ synchronized?
(它在iOS上很少使用..) 无论如何,如果你使用(非原子)然后@synchronized,它似乎有点奇怪..
让我们的Apple编译器更好地完成它的工作:
默认情况下属性是原子默认情况下,是Objective-C属性 是原子的:
@interface XYZObject:NSObject @property NSObject * implicitAtomicObject; //原子默认@property(原子)NSObject * explicitAtomicObject; //明确标记为原子 @end这意味着合成访问器确保值为 始终通过getter方法完全检索或通过 setter方法,即使同时调用访问器 不同的主题。
最后一点:正如Apple所说,使用lock,atomic等,并不能保证不会对不同线程的数据进行深度访问:
如果您有一个数组并且使用atomic / synchronized保护它(它的引用..),则保护对它的访问,而不是对包含的对象的访问。 (你必须小心谨慎地锁定......竞争条件可能发生......)