iphone passing value to other class

2019-06-11 16:12发布

I have one bit of code inside RootViewController.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
 BlogRssParser *blogRss = [[BlogRssParser alloc] init];
 blogRss.terms = [[selectedObject valueForKey:@"data"] description];
 //[blogRss setSelectedObject:selectedObject];
 NSLog(@"%@", blogRss.terms);

 RssFunViewController *rssFun = [[RssFunViewController alloc] initWithNibName:@"RssFunViewController" bundle:nil];
 [self.navigationController pushViewController:rssFun animated:YES];
 [rssFun release];

}

So when the user clicks the row, it goes to RssFunViewController. But it uses BlogRssParser to fill RssFun's tableview. So i'm trying to get between that and send a value from RootView to BlogRss so it fills RssFun with the right data.

i have this in BlogRssParser.m:

-(BOOL)fetchAndParseRss{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //To suppress the leak in NSXMLParser
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];

    NSLog(@"%@", self.terms);

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://news.search.yahoo.com/rss?ei=UTF-8&p=%@&fr=news-us-ss", self.terms]];
    NSLog(@"%@", url);

    BOOL success = NO;
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:YES];
    [parser setShouldReportNamespacePrefixes:YES];
    [parser setShouldResolveExternalEntities:NO];
    success = [parser parse];
    [parser release];
    [pool drain];
    return success;
}

In the console, the log of terms in RootView has the right value. But in BlogRssParser, it comes up as (null). I have declared terms as a property in BlogRssParser.m.

This is how i declared it:

 @interface BlogRssParser : NSObject {
    BlogRss * _currentItem;
    NSMutableString * _currentItemValue;
    NSMutableArray * _rssItems;
    id<BlogRssParserDelegate> _delegate;
    NSOperationQueue *_retrieverQueue;
    //NSManagedObject *selectedObject;
    NSString *terms;
}


@property(nonatomic, retain) BlogRss * currentItem;
@property(nonatomic, retain) NSMutableString * currentItemValue;
@property(readonly) NSMutableArray * rssItems;
//@property(nonatomic,retain) NSManagedObject *selectedObject;
@property(nonatomic, retain) NSString *terms;

@property(nonatomic, assign) id<BlogRssParserDelegate> delegate;
@property(nonatomic, retain) NSOperationQueue *retrieverQueue;

2条回答
Emotional °昔
2楼-- · 2019-06-11 16:40

Have you tried looking at the NSNotificationCenter. This is very helpful for these kinds of situations and can solve problems like this easily. Also, this way you can easily update multiple objects/views with one call.

查看更多
Luminary・发光体
3楼-- · 2019-06-11 16:44

Use self.terms instead. You shouldn't use the underlying ivar apart from inside the accessor methods.

查看更多
登录 后发表回答