繁体   English   中英

从UITableViewController中的类调用方法

[英]Calling a method from a class in a UITableViewController

大家好,大家好

我有一个关于从类中调用UITableViewController的方法的问题,在此方法中:loadData我已连接到本地主机并获取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];

}

如果有人可以帮助我或给我一个很好的指导,我将非常感激。

@interface CLub:NSObject

@property(nonatomic,strong)NSMutableArray *团队;

@property(nonatomic,strong)NSString * teamName;

-(无效)loadData;

@结束

@实现CLub

-(id)init {

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

}

-(BOOL)loadData {BOOL isDataLoaded = NO; NSURL * url = [[NSURL分配] 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;

}

@结束

在viewController中检查是否返回yes

@implementation ClubsTableViewController

  • (void)viewDidLoad {[super viewDidLoad];

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

if(isDataLoadedSuccessFully){//重新加载用户界面} else {}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM