从UITableViewController中的类调用方法

时间:2014-06-13 07:23:58

标签: ios objective-c uitableview

嗨,有最好的成员,

我有一个关于从我的类调用一个方法到UITableViewController的问题,在这个方法中调用:loadData我已经连接到我的localhost并获得一个json对象,这个方法的实现是这样的不需要担心这一点,当我NSLog它给我正确的输出。

奇怪的是,当我在TableViewController中将此方法调用到我的ViewDidLoad方法时,我什么都得不回来,这怎么可能?因为当我将我的方法移动到TableViewController的.h文件中并将其实现到我的TableViewController.m时,它就可以工作。

这是一个例子。

@interface CLub : NSObject

@property(nonatomic,strong) NSMutableArray *teams;
@property(nonatomic,strong) NSString *teamName;
-(void)loadData;

@end

@implementation CLub

-(void)loadData {

    NSURL *url = [[NSURL alloc] initWithString:@"http://localhost/xampp/flashbackapi/"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


        self.club.teams = [[NSMutableArray alloc] init];

        self.club.teams = [responseObject objectForKey:@"clubs"];




    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error %@ %@",error,error.userInfo);
    }];



    [operation start];

}

@end

@implementation ClubsTableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.club = [[CLub alloc]init];
    [self.club loadData];

}
如果有人可以帮助我或给我一个好的方向,我会非常感激的。

1 个答案:

答案 0 :(得分:2)

  

@interface CLub:NSObject

     

@property(非原子的,强大的)NSMutableArray *团队;

     

@property(非原子,强)NSString * teamName;

     

- (无效)loadData;

     

@end

     

@implementation CLub

     

- (ID)INIT {

self = [super init];
if (self) {
 teams = [[NSMutableArray alloc]init];

}
return self;
     

}

     

- (BOOL)loadData {       BOOL isDataLoaded = NO;       NSURL * url = [[NSURL alloc] initWithString:@“”];       NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


    self.club.teams = [[NSMutableArray alloc] init];

    self.club.teams = [responseObject objectForKey:@"clubs"];

    isDataLoaded = YES;
    return isDataLoaded ;



} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error %@ %@",error,error.userInfo);
    isDataLoaded = NO;
    return isDataLoaded ;

}];

[operation start];
return isDataLoaded;
  

}

     

@end

在viewController中检查是否返回yes

  

@implementation ClubsTableViewController

     
      
  • (void)viewDidLoad {   [super viewDidLoad];

         

    self.club = [[CLub alloc] init];   BOOL isDataLoadedSuccessFully = [self.club loadData];

  •   
     

如果(isDataLoadedSuccessFully){   //重新加载UI   }否则{}

     

}