协议和委托不访问方法

时间:2013-05-16 01:27:00

标签: ios uiviewcontroller delegates protocols nsobjectcontroller

我正在尝试将一些数据传回给我的UIView,我将尝试解释它的复杂情况。

我有我的

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

这是我的代码用于设置协议和委托

的样子

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
}

所以我的问题是我在设置此协议/ delgate时缺少什么,因为它无法正常工作?

如果您需要更多信息,我希望问题的结构有意义,请告诉我。

2 个答案:

答案 0 :(得分:1)

改变这个:

#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];

答案 1 :(得分:1)

您的问题是您正在创建SeriesDataClass的2个实例,一个在MainViewController中,另一个在RequestClass中。因此,调用委托方法的实例与主视图控制器将自身设置为委托的实例不同。在MainViewController中创建RequestClass的实例时,需要将seriesDataClass(您创建的实例)传递给RequestClass中的属性,以便它将使用相同的实例。

您应该在MainViewController中创建一个属性:

@property (strong,nonatomic) SeriesDataClass *seriesDataClass;

viewDidLoad方法应如下所示:

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

然后在didSelectRowAtIndexPath中,创建RequestClass实例,并将其传递给seriesDataClass:

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

在RequestClass的.h中,您需要创建该属性:

@property (strong,nonatomic) SeriesDataClass *seriesDataInstance;

然后在RequestClass中,这个:

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

应该只用:

替换
[self.seriesDataInstance recivedData:uncompressedData];

我已经改变了一些大写,以反映你应该这样做的方式。类应以大写字母开头,实例和方法应以小写字母开头。