0

I am trying to pass some data back to my UIView, its abit of a complicated situation I will try to explain it.

I have my

mainViewController // request is made to RequestClass
requestClass // Request is sent off to DB for data, data is returned then passed to seriesDataClass
seriesDataClass // sorts the data and then sends back the needed info to mainViewController using protocol & delegates

This is what my code looks like for setting up the protocols and delegates

mainViewController.h

#import "SeriesDataClass.h"

@interface MatchingSeriesViewController : UIViewController <GetSeriesViewParsedData>
{

mainViewController.m

#import "SeriesDataClass.h"
// this is where I set up my protocols delegate.
- (void)viewDidLoad
{
    //..
    // get delegate ready
    SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];

    [seriesDataClass setDelegate:self];
//..

// pass data over to requestClass so you can get the info from the DB
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
   [requestClass GetSeries:requestID];
//..
}

requestClass.m

// dose a bunch of stuff then calls seriesDataClass and passes all of its information over to it

//.. 
 SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];
[seriesDataClass recivedData:uncompressedData];
//..

seriesDataClass.h

#import <Foundation/Foundation.h>

// This passes data back to the mainViewController
@protocol GetSeriesViewParsedData <NSObject>
- (void)sendGetSeriesArrays:(NSDictionary *)series LSeries:(NSDictionary *)lSeries;
@end

@interface SeriesDataClass : NSObject <NSXMLParserDelegate> {
//..

}
@property (assign, nonatomic) id <GetSeriesViewParsedData> delegate;

seriesDataClass.m

@implementation SeriesDataClass

@synthesize delegate;

// then in a method later on i call the delegate to pass the information back to mainViewController but this dosnt do anything atm.

//..
[self.delegate sendGetSeriesArrays:seriesDictionary LSeries:lSeriesDictionary];
//..

mainViewController.m

// then back in the mainViewController class I Have the method

- (void)sendGetSeriesArrays:(NSDictionary *)series LSeries:(NSDictionary *)lSeries {
// this method is never accessed
}

So my question is what am i missing in the set up of this protocol/delgate for it not to be working correctly?

I hope the structure of the question makes sense if you need any more information please let me know.

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

2 Answers2

1

Change this:

#import "SeriesDataClass.h"
// this is where I set up my protocols delegate.
- (void)viewDidLoad
{
//..
// get delegate ready
SeriesDataClass *seriesDataClass = [[GetSeriesResultItem alloc] init];

[seriesDataClass setDelegate:self];
Arian Sharifian
  • 1,295
  • 11
  • 14
  • okay I have changed that but its still not getting to that method. – HurkNburkS May 16 '13 at 01:49
  • @HurkNburkS, why are you alloc init'ing a SeriesDataClass object with GetSeriesResultItem? And then in your next line you have [SeriesResultItem setDelegate:self]. What is SeriesResultItem? – rdelmar May 16 '13 at 01:56
  • sorry seriesResultClass was the old name which I had written this question for.. then decided to try something I made a new class which is called seriesDataClass which I forgot to update the question code with.. just ignore SeriesResultItem I will change it now.. but im 100% sure my code is calling the correct names in my app. – HurkNburkS May 16 '13 at 02:07
  • I have updated the code in my question and double checked that my code is calling the correct class/method – HurkNburkS May 16 '13 at 02:09
1

Your problem is that you are creating 2 instances of your SeriesDataClass, one in MainViewController, and another in RequestClass. So, the instance that calls the delegate method is not the same instance that the main view controller set itself as the delegate of. When you create an instance of RequestClass in MainViewController, you need to pass seriesDataClass (the instance you created) to a property in RequestClass, so that it will be working with the same instance.

You should create a property in MainViewController:

@property (strong,nonatomic) SeriesDataClass *seriesDataClass;

The viewDidLoad method should then look like this:

- (void)viewDidLoad {
    self.seriesDataClass = [[SeriesDataClass alloc] init];
    [self.seriesDataClass setDelegate:self];
}

Then in didSelectRowAtIndexPath, create your RequestClass instance, and pass it seriesDataClass:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   RequestClass *requestClass = [[RequestClass alloc] init];
   requestClass.seriesDataInstance = self.seriesDataClass;
   [requestClass GetSeries:requestID];
}

In the .h of RequestClass you need to have created that property:

@property (strong,nonatomic) SeriesDataClass *seriesDataInstance;

Then in RequestClass, this:

SeriesDataClass *seriesDataClass = [[SeriesDataClass alloc] init];
[seriesDataClass recivedData:uncompressedData];

should be replaced with just:

[self.seriesDataInstance recivedData:uncompressedData];

I've changed some of the capitalization, to reflect the way you should do it. Classes should start with capital letters, and instances and methods should start with lowercase ones.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • do you have any clues on how to do this I dont even know what to search for on google that is along the same lines as the problem I am having. – HurkNburkS May 16 '13 at 02:38
  • okay cool going to read over it slowly and then try to implement it i might be 15 minutes :P thanks for the edit :P – HurkNburkS May 16 '13 at 02:45