我想在另一个类中获取一个类数组数据(即我想在mainview类中获取connectionDidFinishLoading数组数据)
背景类.h
#import <UIKit/UIKit.h>
#import "JSON.h"
@interface BackGroundClass : UIViewController<NSURLConnectionDataDelegate>
@end
BackGroundClass.m
@interface BackGroundClass ()
{
NSMutableData * webData;
NSURLConnection * connection;
NSMutableArray * array;
}
@end
@implementation BackGroundClass
- (void)viewDidLoad {
array = [[NSMutableArray alloc]init];
NSURL * url = [NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webData = [[NSMutableData alloc]init];
}
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error is %@",[error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSDictionary * responseString = [allDataDictionbary JSONValue];
NSArray * allTweets = [responseString objectForKey:@"loans"];
for (NSDictionary * obj in allTweets) {
NSDictionary * act=[obj objectForKey:@"location"];
NSDictionary * act1 = [ act objectForKey:@"geo"];
NSString * level = [act1 objectForKey:@"pairs"];
//NSDictionary * act = [obj objectForKey:@"description"];
//NSArray * languages = [act objectForKey:@"languages"];
//NSString * name = [[languages objectAtIndex:obj]objectForKey:@"name"];
[array addObject:level];
}
NSLog(@"act array is %@",array);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
Now connectionDidFinishLoading methods inside array data i want to get in mainView class how can i get please help me some one else
主视图.h
#导入
@interface ViewController2 : UIViewController
@结束
主视图.m
导入“ViewController2.h”
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
最佳答案
通知
您可以使用 NSNotificationCenter
从您的连接类发送通知。并在主视图 Controller 中向通知添加一个观察者。通过 NSNotification userInfo 字典和通知对象中的对象 arg 传递数据有两种选择。
http://nshipster.com/nsnotification-and-nsnotificationcenter/
委派
创建一个委托(delegate)并将主 VC 指定为委托(delegate)并使用从响应中获得的数据调用委托(delegate)方法
http://www.tutorialspoint.com/ios/ios_delegates.htm
熟悉上述概念后,请查看以下选项
block
http://www.appcoda.com/objective-c-blocks-tutorial/
RAC 信号( ReactiveCocoa )
http://nshipster.com/reactivecocoa/
编辑 - 使用委托(delegate)
BackGroundClass.h
@protocol MyConnectioDelegate <NSObject>
//using id type here, so NSArray or NSDictionary objects can be passed
-(void)didRecieveResponse:(id)aCollection;
@end
@interface BackGroundClass : UIViewController<NSURLConnectionDataDelegate>
@property(weak, nonatomic)id<MyConnectioDelegate>delegate;
@end
BackGroundClass.m
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
....
//Once you get the collection.
[_delegate didRecieveResponse:<theCollection>];
}
ViewController2.m
@interface ViewController2 ()< MyConnectioDelegate >
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
//Hope you already have the object of BackGroundClass created
_backgroundClassObject.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)didRecieveResponse:(id)aCollection{
//You have the collection sent from background class
}
@end
关于ios - 我们如何在另一个类中的数组或字典数据中获取 SBJSON connectionDidFinishLoading 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951378/