设置/从单例中检索数据

时间:2012-11-26 21:32:36

标签: ios singleton

Apple拒绝了我们的应用选项,因为该页面加载选项卡之间的时间太长。在我简单地调用webview来显示通过CMS管理的内容之前。现在我们已经实现了JSON,我想使用单例设计模式预加载5个选项卡的数据。我似乎无法设置单例值,正如我在示例中看到的那样。代码:

header.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {
    NSString *someProperty;
    ...
}

@property (nonatomic, retain) NSString *someProperty;


+ (id)sharedManager;

@property (strong, nonatomic) NSString* tab3data;

@end

Implementation.m

//Create a seperate thread to download JSON thread
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1

//Set JSON URL
#define GWDiOSURL [NSURL URLWithString:@"http://m.web.org/cms_mapper.php"]

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize someProperty;


- (id)init {
    if (self = [super init]) {
        someProperty = @"Default Property Value";
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    FirstViewController *sharedManager = [FirstViewController sharedManager];

    NSLog(@"Toll%@",sharedManager);
    // Do any additional setup after loading the view, typically from a nib.
    //Get JSON and load into 'data'
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:GWDiOSURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });

}

//Begin JSON Data Parsing and loading
- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;

    //Parse JSON
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData

                          options:kNilOptions
                          error:&error];

    // Load JSON into a dictionary
    NSDictionary *tabData = [json objectForKey:@"mapper"];

    // Get Tab3 data from dictionary
    NSDictionary *tab3 = [tabData objectForKey:@"#tab3_content"];

    // Load Tab3 data into a string from dictionary
    NSString *html = [NSString stringWithFormat:@"%@",tab3];

    // Verify content via counsel
    //NSLog(@"Second Data:%@",html);

    // Load content into webView
    [webView loadHTMLString:html baseURL:nil];

[FirstViewController sharedManager].someProperty = @"asdf";

}


+ (id)sharedManager {
    static FirstViewController *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;

}

我需要将html的值设置为单例。以下行

[FirstViewController sharedManager].someProperty = @"asdf";

产生此错误

Propery 'someProperty' not found on object of type 'id'.

我一直试图让这整个过程工作好几天......我很欣赏这种见解。

1 个答案:

答案 0 :(得分:0)

那么,你的类方法sharedManager会返回一个id。尝试在sharedManager中返回FirstViewController *。

+(FirstViewController *)sharedManager;