Navid
Navid

Reputation: 495

Calling a method from a class in a UITableViewController

Hi there best members,

i've a question about calling a method from my class into the UITableViewController, in this method called: loadData i've made connection to my localhost and getting a json object, the implementation of this method works so don't need to worry about that and when i NSLog it gives me the correct output.

The strange thing is when i'm calling this method into my ViewDidLoad method in my TableViewController i don't get anything back, how is this possible? Because when i'm move my method into the .h file of TableViewController and implement it to my TableViewController.m then it works.

Here an example.

@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];

}

I would be very greatfull if there is someone who can help me out or give me a good direction.

Upvotes: 0

Views: 50

Answers (1)

Jayaprada
Jayaprada

Reputation: 954

@interface CLub : NSObject

@property(nonatomic,strong) NSMutableArray *teams;

@property(nonatomic,strong) NSString *teamName;

-(void)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

In the viewController check if returns yes

@implementation ClubsTableViewController

  • (void)viewDidLoad { [super viewDidLoad];

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

if(isDataLoadedSuccessFully){ //Reload UI }else{}

}

Upvotes: 2

Related Questions