Ios 将NSObject类中的值传递回UIViewController
我不知道为什么这么难,但我不能让它工作。以下是我的基本流程: 我有一个带有子视图UIView的UIViewController,它本身有一个子视图UIView按钮。单击该按钮将实例化名为TwitterController的NSObject的新实例,为twitter提要创建NSURL,然后将控制权交给TC进行URLConnection并序列化返回的数据 以下是ViewController中的相关代码(Pruit_Igoe是我,尽管我没有发布太多:D),但请随意跟随):Ios 将NSObject类中的值传递回UIViewController,ios,objective-c,cocoa-touch,methods,uiviewcontroller,Ios,Objective C,Cocoa Touch,Methods,Uiviewcontroller,我不知道为什么这么难,但我不能让它工作。以下是我的基本流程: 我有一个带有子视图UIView的UIViewController,它本身有一个子视图UIView按钮。单击该按钮将实例化名为TwitterController的NSObject的新实例,为twitter提要创建NSURL,然后将控制权交给TC进行URLConnection并序列化返回的数据 以下是ViewController中的相关代码(Pruit_Igoe是我,尽管我没有发布太多:D),但请随意跟随): showTwitterFe
- showTwitterFeed转储视图vTwitterFeed中的对象(关闭视图、图像等的按钮)
- getTwitterFeed开始NSURLConnection过程
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
//dump it into an array
tweetArray = [[NSMutableArray alloc] init];
for(NSDictionary* thisTweetDict in jsonArray) {
NSString* tweet = [thisTweetDict objectForKey:@"text"];
[tweetArray addObject:tweet];
}
}
这一切都很好,记录tweetArray和所有文本都在那里,记录TweetDict和Twitter发送回来的所有数据都在那里。问题是我想将tweetArray传递回ViewController,但我似乎不知道如何传递
我已经做了以下工作:
尝试从getTwitterFeed返回TweetArray,但返回结果为null(我猜是该方法在连接完成之前返回了数组)
尝试将其置于UserDefaults中,但我一直得到null(与上面的猜测相同,但随后我将其置于ConnectiondFinish中,仍然为null)
试图将对ViewController的引用传递给TwitterController,然后在VC中调用一个方法将数组传递给TwitterController,但在TwitterController中,我出错了,因为它说我的VC实例无法识别选择器。(就在那儿,我检查了三遍)
我相信这是很简单的,我只是有点糊涂,但有人能帮我吗
编辑:以下是我如何将其传回VC的:
我将使用此方法将VC传递给TC(这是在VC中)
在VC.h中,我有一个UIViewController*thisViewController
在showTwitterFeed的VC.m中:
- (void) showTwitterFeed : (UIView* ) theTwitterView : (UIViewController* ) theViewController {
thisViewController = theViewController;
//...other code to build view objects
然后在
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
...
for(NSDictionary* thisTweetDict in jsonArray) {
NSString* tweet = [thisTweetDict objectForKey:@"text"];
[tweetArray addObject:tweet];
}
[thisViewController getTwitterFeed:tweetArray]; //<--this would error out saying selector not available
在VC.m
- (void) getTwitterFeed : (NSArray* ) theTwitterFeed {
NSLog(@"%@", theTwitterFeed);
}
您无法从
getWitterFeed
返回它,因为尚未调用connectiondFinishLoading
您需要做的是在TwitterController
中设置协议,并使您的ViewController
成为TwitterController
的代理
然后,当发生connectiondFinishLoading
并保存twitter信息时,您可以将该函数调用回您的代理(查看控制器)
在TwitterController
协议中创建一个名为twitterDataReceived:(NSDictionary*)tweetDict
的函数,并在ConnectiondFinishLoading
函数中调用它:[self.delegate twitterDataReceived:thisTweetDict]
或者如果您只想发送文本,则将其设置为twitterTextReceived:(NSString*)theweet
并调用[self.delegate twitterTextReceived:tweet]
或使用类似于twitterArrayReceived:(NSArray*)tweetArray
和[self.delegate twitterArrayReceived:tweetArray]的数组代码>,或任何您想发回的内容
如果您不熟悉设置协议和代理,有许多问题可以帮助您解决,例如:您得到一个无法识别的选择器,因为thisViewController
属于UIViewController
类型,但没有定义此方法(虽然从您发布的代码中看不到它,但我绝对肯定这种情况,因为在分配thisViewController=theViewController
和theViewController
类型为UIViewController
时,您会遇到编译错误。)
因此,将thisViewController
类型更改为自定义视图控制器,并更改
(无效)显示TwitterFeed:(UIView*)WitterView:(UIViewController*)ViewController
将是:
(无效)showTwitterFeed:(UIView*)WitterView:(*)ViewController
你能把你试图把tweetArray放在视图控制器中的代码贴出来吗?及其定义?是的,请稍等几秒钟,我会把它放在。。。
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
...
for(NSDictionary* thisTweetDict in jsonArray) {
NSString* tweet = [thisTweetDict objectForKey:@"text"];
[tweetArray addObject:tweet];
}
[thisViewController getTwitterFeed:tweetArray]; //<--this would error out saying selector not available
- (void) getTwitterFeed : (NSArray* ) theTwitterFeed;
- (void) getTwitterFeed : (NSArray* ) theTwitterFeed {
NSLog(@"%@", theTwitterFeed);
}