English 中文(简体)
在单州一级使用阿雷拉通过数据吗?
原标题:Using Arrays in Singleton Class to Pass Data Between View Controllers IOS?

我一直在寻找桌子和单吨级,但我无法在此找到任何解决办法。

在用户选择一行时,我有一个看法,即我想将所选数据发送到一个单州级的阵列,并打印在另一个观点控制器中加以筛选。

这里是我的单州级法典:

#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];
最佳回答
  • Don t make standLoc readonly if you want to update it from other classes.
  • Why do you want standLoc to be an array if you re going to pass it one object out of stands ?
  • 你们想要的那类yn(在上述两点之后)是:

    数据主计长* 共享数据库=[数据交换股];

    共享Singleton.standLoc = / 无论数据内容是什么,都应在单一州进行;

问题回答

为什么你们需要单一州? 如果你把数据直接从一个观点控制者传递到另一个观点,那么你的代码就会更加简单,更容易维持。 如你再次使用导航控制器,这样,当你桌上的一个囚室被利用时,该表的观察控制器可以把一个新的细节控制器推向另一个细节控制器。

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




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签