我一直在寻找桌子和单吨级,但我无法在此找到任何解决办法。
在用户选择一行时,我有一个看法,即我想将所选数据发送到一个单州级的阵列,并打印在另一个观点控制器中加以筛选。
这里是我的单州级法典:
#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
因此,如果我把数据传递给一等单一州级的阿雷拉,那么怎么办。
- (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;
}
在辩论会上,我可以看到选定的项目是共享的Singleton,但我如何将其通过到NSArray*的会址。
http://www.ohchr.org。
SO i编辑了我的代码,现在它用精彩的多位主计长的观点处理。
我:
#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;
}
}
a) 将数据传送至单一州:
DataController* sharedSingleton = [DataController sharedInstance];
NSString* transfer = [stands objectAtIndex:indexPath.row];
[sharedSingleton setData:transfer];