我不知道为什么这很困难,但我无法得到它的工作。这是我的基本流程:从NSObject类传递一个值到UIViewController
我有一个UIViewController与一个子视图UIView本身有一个子视图UIButton。单击按钮实例化一个名为TwitterController的NSObject新实例,为twitter提要创建一个NSURL,然后将控制权交给TC进行URLConnection并序列化返回的数据。
下面是视图控制器的相关代码(Pruit_Igoe是我,随时跟踪,虽然我不太发布: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 the twitter view
[self toggleView:vTwitterFeed];
[self toggleView:vContactCard];
}
- showTwitterFeed转储视图vTwitterFeed的对象(按钮关闭视图,图像等)
- getTwitterFeed开始NSURLConnection的过程
在TwitterController,我得到了Twitter的饲料,并在这里对其进行处理:
- (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和所有的文字是存在的,登录thisTweetDict和所有的Twitter发回大量的数据中是存在的。问题是我想通过tweetArray回到ViewController,但我似乎无法弄清楚如何。
我已经做了以下内容:
试图从getTwitterFeed返回TweetArray但回来为空(我的猜测是该方法返回的阵列之前连接过完)
试图把它UserDefaults,但我保持null(与上面相同的猜测,但后来我把它放在connectionDidFinish,仍然为空)
试图将ViewController的引用传递给TwitterController,然后调用VC中的方法将数组传递给但在TwitterController我错误了,因为它说我的VC实例不识别选择器。 (它在那里,我已经三重检查)。
我相信这很简单,我只是很密集,但有人可以帮助我呢?
编辑:这就是我试图通过它回到VC:
我会VC.h通过VC采用这种方法TC(这是VC)
[twitterManager showTwitterFeed:vTwitterFeed:self];
我有一个*的UIViewController thisViewController
在VC.m在showTwitterFeed:
- (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.h
- (void) getTwitterFeed : (NSArray*) theTwitterFeed;
和VC。米
- (void) getTwitterFeed : (NSArray*) theTwitterFeed {
NSLog(@"%@", theTwitterFeed);
}
您可以张贴在那里你试图把tweetArray在视图控制器的代码?及其定义? – giorashc
是的,坚持几秒钟,我会在... – PruitIgoe