Search code examples
iosuiviewcontrollerdelegatesprotocolsnsobjectcontroller

protocol and delegate not accessing method


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.


Solution

  • 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.