开发者

iphone passing value to other class

开发者 https://www.devze.com 2023-01-24 17:18 出处:网络
I have one bit of code inside RootViewController.m: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

I have one bit of code inside RootViewController.m:

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

 NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:ind开发者_如何学PythonexPath];
 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;


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消