0

I have just solved another problem regarding the same piece of code but I want to separate my 2 problems.

I want to store in my AppDelegate the table generated by an Rss parser so that it is not necesary to do it again each time the viewcontroller that displays the list is loaded, To do so, I have the following code:

AppDelegate.h

#import <Three20/Three20.h>

#import "NewsRssParser.h"
#import "NewsRss.h"

@class NewsRssParser;
@class NewsRss;

@interface AppDelegate : NSObject <UIApplicationDelegate,NewsRssParserDelegate> {
    NewsRssParser * _rssParser; 
    NSMutableArray * _newsRssArray;
}

@property(readwrite, retain) NewsRssParser * rssParser;
@property(readwrite, retain) NSMutableArray * newsRssArray;

@end

ApDelegate.m

[...]

@implementation AppDelegate

@synthesize rssParser = _rssParser; @synthesize newsRssArray = _newsRssArray;

[...]

At a certain stage of execution of the app, the table is properly populated by the Rss parser. When this happens, I want to transform it into a format that can be displayed in another view controller:

  • (void)myFunction{

    NSMutableArray *dsItems = [[[NSMutableArray alloc] init] autorelease];

    for(NewsRss *rssItem in self.rssParser.rssItems) { NSString * rssItemTitle = [rssItem title]; NSString * rssItemAuthor = [@"par " stringByAppendingString:[rssItem author]]; NSString * rssItemDescription = [rssItem description]; NSString * rssItemLinkUrl = [rssItem linkUrl]; NSString * rssItemImageUrl = [rssItem mediaUrl];

    TTTableMessageItem *tMsgItem = [TTTableMessageItem itemWithTitle:rssItemTitle caption:rssItemAuthor text:rssItemDescription timestamp:nil imageURL:rssItemImageUrl URL:rssItemLinkUrl];
    [self.newsRssArray addObject:tMsgItem];
    [dsItems addObject:tMsgItem];
    

    }

}

I controlled that this works by showing the [_newsRssArray count] in gdb with NSLog. It returns 10 which is the number of articles I have in my RSS feed.

This is when I want to load my ViewController to show the list of articles. I do it this way:

NewsVc.h

#import <Three20/Three20.h>

@protocol NewsVcDelegate;

@class AppDelegate;

@interface NewsVc : TTTableViewController {
    id<NewsVcDelegate> _delegate;

    AppDelegate * _appDelegate;
}

@property(nonatomic,assign) id<NewsVcDelegate> delegate;
@property(nonatomic, retain) AppDelegate * appDelegate;

@end

@protocol NewsVcDelegate <NSObject>

- (void)NewsVc:(NewsVc*)controller didSelectObject:(id)object;

@end

NewsVc.m

[...]

- (void)loadNewsFromParser {

    self.tableView.allowsSelection = NO;

    //reload the table view
    TTListDataSource *ds = [[TTListDataSource alloc] autorelease];
    //NSMutableArray *dsItems = [[[NSMutableArray alloc] init] autorelease];

    NSLog(@"NewsVC(loadNewsFromParser):count/newsRssArray=%d", [self.appDelegate.newsRssArray count]);
    NSLog(@"NewsVC(loadNewsFromParser):count/rssItems=%d", [self.appDelegate.rssParser.rssItems count]);

    [ds initWithItems:self.appDelegate.newsRssArray];

    self.dataSource = ds;
    self.tableView.allowsSelection = YES;

}

[...]

But in this function of the ViewController, I see the array empty, as well as when I try to access directly the parsed data of the NewsRss. What it wrong on my code of AppDelegate/NewsVc to be able to access this array from NewsVc?

Thanks for your help ! I feel very slow today stuck on this...

1 Answer 1

5

Where do you assign appDelegate? There must be a line like this somewhere in your view controller initialization:

self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, indeed i missed that other piece of code...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.