ios - Passing a value from NSObject class back to UIViewController -
i don't know why being difficult can't work. here's basic flow:
i have uiviewcontroller subview uiview has subview uibutton. clicking button instantiates new instance of nsobject called twittercontroller, creates nsurl twitter feed , hands control on tc urlconnection , serialize data returned.
here's relevant code in viewcontroller (pruit_igoe me, feel free follow though don't post : d) :
- (void) gettwitter { //load new manager twittermanager = [twittercontroller new]; [twittermanager showtwitterfeed:vtwitterfeed:self]; nsurl* twitterfeedpath = [nsurl urlwithstring: @"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=pruit_igoe"]; [twittermanager gettwitterfeed:twitterfeedpath]; //toggle twitter view [self toggleview:vtwitterfeed]; [self toggleview:vcontactcard]; }
- showtwitterfeed dumps objects in view vtwitterfeed (button close view, images, etc.)
- gettwitterfeed begins nsurlconnection process
in twittercontroller, twitter feed , process here:
- (void)connectiondidfinishloading:(nsurlconnection *)theconnection { //do data! nserror *e = nil; //parse json data nsarray *jsonarray = [nsjsonserialization jsonobjectwithdata: receiveddata options: nsjsonreadingmutablecontainers error: &e]; //dump array tweetarray = [[nsmutablearray alloc] init]; for(nsdictionary* thistweetdict in jsonarray) { nsstring* tweet = [thistweetdict objectforkey:@"text"]; [tweetarray addobject:tweet]; } }
this works fine, log tweetarray , text there, log thistweetdict , ton of data twitter sends there. problem want pass tweetarray viewcontroller can't seem figure out how to.
i've done following:
tried returning tweetarray gettwitterfeed came null (my guess method returned array before connection had finished)
tried put in userdefaults keep getting null (same guess above, put in connectiondidfinish , still null)
tried pass reference viewcontroller twittercontroller , call method in vc pass array in twittercontroller error out because says instance of vc doesn't recognize selector. (it's there, i've triple checked).
i sure simple , being dense me this?
edit: here's how tried pass vc:
i pass vc tc using method (this in vc)
[twittermanager showtwitterfeed:vtwitterfeed:self];
in vc.h had uiviewcontroller* thisviewcontroller
in vc.m in showtwitterfeed:
- (void) showtwitterfeed : (uiview* ) thetwitterview : (uiviewcontroller* ) theviewcontroller { thisviewcontroller = theviewcontroller; //...other code build view objects
then in
- (void)connectiondidfinishloading:(nsurlconnection *)theconnection { ... for(nsdictionary* thistweetdict in jsonarray) { nsstring* tweet = [thistweetdict objectforkey:@"text"]; [tweetarray addobject:tweet]; } [thisviewcontroller gettwitterfeed:tweetarray]; //<--this error out saying selector not available
back in vc.h
- (void) gettwitterfeed : (nsarray* ) thetwitterfeed;
and in vc.m
- (void) gettwitterfeed : (nsarray* ) thetwitterfeed { nslog(@"%@", thetwitterfeed); }
you can't return gettwitterfeed
because connectiondidfinishloading
has not been called yet.
what need set protocol in twittercontroller
, make viewcontroller
delegate
of twittercontroller
.
then when connectiondidfinishloading
occurs , save twitter information can call function delegate
(the viewcontroller
).
create function called twitterdatareceived:(nsdictionary *)tweetdict
in twittercontroller
protocol , call in connectiondidfinishloading
function: [self.delegate twitterdatareceived:thistweetdict];
or if want send text make twittertextreceived:(nsstring *)thetweet
, call [self.delegate twittertextreceived:tweet];
or use array twitterarrayreceived:(nsarray *)tweetarray
, [self.delegate twitterarrayreceived:tweetarray];
, or whatever want send back.
if unfamiliar setting protocol , delegate there many questions available out, one:objective-c protocol delegates
Comments
Post a Comment