ViewController的实例返回nil

时间:2017-04-28 05:19:36

标签: ios objective-c

我有HomeViewController的实例如下。我正在尝试访问其属性,其中包含数据集。但是,每当我签入其他课程时,GlobalFunctions所有HomeViewController属性都为零。

HomeViewController.h

@property (nonatomic, strong) NSMutableArray *pTempElements;
+(HomeViewController*) homeDataInstance;

HomeViewController.m

+(HomeViewController*) homeDataInstance {
    static HomeViewController *dataInstance;
    @synchronized(self) {
        if(!dataInstance){
            dataInstance = [[HomeViewController alloc] init];
        }
    }
    return dataInstance;
}

-(void)loadFromURL {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSString *PRODUCT_ALL_URL = [settings objectForKey: @"PRODUCT_ALL_URL"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager GET:PRODUCT_ALL_URL parameters:nil progress: nil success:^(NSURLSessionTask * _Nonnull operation, id responseObject) {
        if(responseObject != NULL) {
            ProductData *dicItem = [[ProductData alloc]init];
            for ( id jsonItem in responseObject)
            {
                dicItem = [[ProductData alloc]initWithDictionary:jsonItem];
                [self.pElements addObject:dicItem];

            }
            self.pTempElements = pElements;
            [self.productCollectionView reloadData];
        }

    } failure:^(NSURLSessionTask * _Nullable operation, NSError * _Nonnull error) {
    }];
}

GlobalFunctions.m

HomeViewController *restaurantData = [HomeViewController homeDataInstance];
// the following is nil
NSLog(@"%@", restaurantData.pTempElements);
restaurantData类中,

GlobalFunctions全部如下。

enter image description here

4 个答案:

答案 0 :(得分:1)

从您共享的代码段和屏幕截图中,我们可以清楚地看到您的实例已成功创建。

属性值显示为nil,因为您尚未为它们指定任何值。尝试为这些属性分配初始值。

对于UI相关属性,您应该将它们映射到.xib或.storyboard文件中,以便使用某些值初始化它们,或者在使用它们之前在代码中初始化它们。

例如在您的情况下:productCollectionView和categoryCollectionView

同样包含其他属性。

希望我的回答有所帮助!

答案 1 :(得分:0)

你的问题是单身对象。试试这个类方法:

+(instancetype)sharedInstance{

    static storeViewController *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[storeViewController alloc] init];
    });

    return sharedInstance;
}

答案 2 :(得分:0)

试试这个!

#import "WebService.h"
static WebService *webService;
@implementation WebService
+(WebService *)sharedWebService{
    if(!webService){
        webService = [[WebService alloc] init];
    }
    return webService;
}

在.h文件中

#import <Foundation/Foundation.h>

@interface WebService : NSObject

+(WebService *)sharedWebService;
 @property (nonatomic, strong) NSMutableArray *pTempElements;

答案 3 :(得分:0)

使用块

尝试此功能
-(void)loadFromURLWithCallback:(void(^)(NSArray *pTempElements))callback {
    // add this condition if your dont need to refresh data
    if (self.pElements) {
        callback(self.pElements)
        [self.productCollectionView reloadData];
        return;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
    NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSString *PRODUCT_ALL_URL = [settings objectForKey: @"PRODUCT_ALL_URL"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager GET:PRODUCT_ALL_URL parameters:nil progress: nil success:^(NSURLSessionTask * _Nonnull operation, id responseObject) {
        if(responseObject != NULL) {
            ProductData *dicItem = [[ProductData alloc]init];
            for ( id jsonItem in responseObject)
            {
                dicItem = [[ProductData alloc]initWithDictionary:jsonItem];
                [self.pElements addObject:dicItem];

            }
            self.pTempElements = pElements;
            [self.productCollectionView reloadData];
            callback(elf.pTempElements)

        }

    } failure:^(NSURLSessionTask * _Nullable operation, NSError * _Nonnull error) {
        callback(nil);

    }];
}

调用方法

[self loadFromURLWithCallback:^(NSArray *pTempElements) {

    }];