ios - How can we get SBJSON connectionDidFinishLoading method inside array or Dictionary data in another class -
i want 1 class array data in class(i.e connectiondidfinishloading array data want in mainview class)
backgroundclass.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 %@",[error localizeddescription]); } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nsstring * alldatadictionbary = [[nsstring alloc] initwithdata:webdata encoding:nsutf8stringencoding]; nsdictionary * responsestring = [alldatadictionbary jsonvalue]; nsarray * alltweets = [responsestring objectforkey:@"loans"]; (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 %@",array); } - (void)didreceivememorywarning { [super didreceivememorywarning]; } @end connectiondidfinishloading methods inside array data want in mainview class how can please me 1 else
mainview.h
#import
@interface viewcontroller2 : uiviewcontroller
@end
mainview.m
import "viewcontroller2.h"
@interface viewcontroller2 () @end @implementation viewcontroller2 - (void)viewdidload { [super viewdidload]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } @end
notification
you can use nsnotificationcenter
send notification connection class. , in main view controller add observer notification. there 2 options pass data via nsnotification userinfo dictionary & object arg in notification object.
http://nshipster.com/nsnotification-and-nsnotificationcenter/
delegation
create delegate , assign main vc delegate & call delegate method data response
http://www.tutorialspoint.com/ios/ios_delegates.htm
once familiar above concepts have @ following options
blocks
http://www.appcoda.com/objective-c-blocks-tutorial/
rac signals (reactive cocoa)
http://nshipster.com/reactivecocoa/
edit - using delegate
backgroundclass.h
@protocol myconnectiodelegate <nsobject> //using id type here, nsarray or nsdictionary objects can passed -(void)didrecieveresponse:(id)acollection; @end @interface backgroundclass : uiviewcontroller<nsurlconnectiondatadelegate> @property(weak, nonatomic)id<myconnectiodelegate>delegate; @end
backgroundclass.m
- (void)connectiondidfinishloading:(nsurlconnection *)connection { .... //once collection. [_delegate didrecieveresponse:<thecollection>]; }
viewcontroller2.m
@interface viewcontroller2 ()< myconnectiodelegate > @end @implementation viewcontroller2 - (void)viewdidload { [super viewdidload]; //hope have object of backgroundclass created _backgroundclassobject.delegate = self; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } -(void)didrecieveresponse:(id)acollection{ //you have collection sent background class } @end
Comments
Post a Comment