0

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

头文件.h

#import <UIKit/UIKit.h>

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

@property (nonatomic, retain) NSString *someProperty;


+ (id)sharedManager;

@property (strong, nonatomic) NSString* tab3data;

@end

实现.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'.

几天来,我一直试图让整个过程正常工作。我很欣赏这种洞察力。

4

1 回答 1

0

好吧,您的类方法 sharedManager 返回一个 id。尝试在 sharedManager 中返回 FirstViewController*。

+ (FirstViewController *)sharedManager;

于 2012-11-26T21:48:10.653 回答