简体   繁体   English

在nsobject和viewcontroller之间传递数据的最佳方法

[英]Best way to pass data between nsobject and a viewcontroller

I have an helper class which has a function that makes an api call and get some json data and formats and returns an array. 我有一个帮助程序类,该类具有进行api调用并获取json数据和格式并返回数组的函数。 My TableViewController is trying to access that returned array. 我的TableViewController试图访问该返回的数组。 Yes, as you expected my tableviewcontroller viewDidLoad method is not able to access the array object returned by my helper class. 是的,正如您期望的那样,我的tableviewcontroller viewDidLoad方法无法访问由我的助手类返回的数组对象。

@interface MyHelperClass : NSObject

@property (nonatomic,retain)NSArray *myArray;

@end

@implementation MyHelperClass

@synthesize myArray;

- (NSArray *) returnArray{

 // make api calls and return array

  return myArray;

}

@end

@implementation MyTableViewController
{
- (void)viewDidLoad
{
    [super viewDidLoad];

    MyHelperClass *myhelper = [[MyHelperClass alloc]initWithPath:getSpacePath];

    allTopics = (NSArray *)[myhelper returnArray];

    NSLog(@"Load my Array%@",allTopics);
}
}

My question is, do I need to implement a delegate to pass the data around or is there any other way to pass around the data to my view controller? 我的问题是,我是否需要实现一个委托以将数据传递给其他人,或者是否有其他方法将数据传递给我的视图控制器?

PS : I do not want to use global variable PS:我不想使用全局变量

Did this code give you any warning ? 这段代码是否给您任何警告?

You are trying to return an NSArray * from void returning method. 您正在尝试从void返回方法返回NSArray *

Modify it to 修改为

- (NSArray *) returnArray{ // YOU CAN RETURN id AS WELL, AS YOU ARE TYPE CASTING THE RESULT AT CALLING TIME
    // make api calls and return array
    NSLog (@"myArray :: %@", [myArray description]); // Post the output back here
    return myArray;
}

Let me know if the problem persists. 让我知道问题是否仍然存在。

EDIT 编辑

Set breakpoints at 将断点设置在

allTopics = (NSArray *)[myhelper returnArray]; // AT - (void)viewDidLoad

and

return myArray; // AT HelperClass method

If first one it getting fired first, then You have to implement as @A-Live suggested in the comment. 如果是第一个被首先触发,那么您必须按照注释中建议的@ A-Live来实现。

Sorry for posting the answer so late. 很抱歉这么晚发布答案。 I figured out what the problem is. 我想出了问题所在。 As @A-Live mentioned, the Rest API calls using AFNetworking is using async calls and hence it's not returning the array to the main thread within it's execution time. 正如@ A-Live所提到的,使用AFNetworking的Rest API调用使用的是异步调用,因此它不会在执行时间内将数组返回到主线程。 In my case, 就我而言

-(void)viewDidLoad {

NSLog(@"I get called first");

MyHelper *helper = [[MyHelper alloc]init];

// returns array. However, [helper getData] is an async call under the hood. Hence myArray is nil
myArray = [helper getData];

}

To solve this problem, I took advantage of NSNotification. 为了解决这个问题,我利用了NSNotification。

@implementation MyHelper{

   -(NSArray *)getData(){

    [[NSNotificationCenter defaultCenter] postNotificationName:@"some.name.notification" object:JSON];

    }
}

-(void)viewDidLoad(){

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData:) name:@"some.name.notification" object:nil];

}

-(void)loadData:(NSNotification *)notif {

// You can access the JSON object passed by the helper in here

NSArray *myArray = [notif object];

// do whatever you want with the array.

}

I hope I am detailed enough. 我希望我足够详细。 I hope this helps someone and saves a lot of headache. 我希望这可以帮助某人并节省很多头痛。

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

相关问题 在Segues与ViewController之间传递数据的最佳方法 - Best way to pass data between ViewController with segues 将数据视图控制器传递给NSObject - Pass data viewcontroller to NSObject 单击TableViewCell时如何将数据从NSObject传递到另一个ViewController? - How to pass data from NSObject to another ViewController when click on TableViewCell? 存储iPhone数据的最佳方法(自定义NSObject) - Best way to store iPhone data (custom NSObject) 如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据 - How to pass data between a UIViewController and an NSObject in Swift 如何在ViewController和子视图ViewController之间传递数据 - How to pass data between a ViewController and a subview ViewController 在视图之间传递数据的最佳方法是什么? - What is the best way to pass data between views? 在 ViewController 和 TabBarController 之间传递数据 - Pass data between ViewController and TabBarController 如何在NSObject类和ViewController之间切换 - How to switch between NSObject Class to ViewController 在MonoTouch中的视图控制器之间传递数据的最佳方法(缓存数据) - Best way to pass data between viewcontrollers (Cache data) in MonoTouch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM