2013-05-16 107 views
0

我想通过一些数据回到我的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的设置什么?

我希望问题的结构是有道理的,如果你需要更多的信息,请让我知道。

回答

1

你的问题是,你正在创建你的SeriesDataClass的两个实例,一个在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]; 

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

+0

您是否有任何关于如何执行此操作的线索我甚至不知道在Google上搜索的内容与我遇到的问题。 – HurkNburkS

+0

@HurkNburkS,看我的编辑。 – rdelmar

+0

好吧凉爽去慢慢阅读它,然后尝试实施它我可能是15分钟:P感谢编辑:P – HurkNburkS

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]; 
+0

好吧我改变了,但它仍然没有得到该方法。 – HurkNburkS

+0

@HurkNburkS,你为什么要用GetSeriesResultItem来初始化一个SeriesDataClass对象?然后在你的下一行你有[SeriesResultItem setDelegate:self]。什么是SeriesResultItem? – rdelmar

+0

对不起seriesResultClass是我已经写了这个问题的旧名称..然后决定尝试一些我做了一个叫做seriesDataClass的新类,我忘了更新问题代码..只是忽略SeriesResultItem我现在会改变它。 。但我100%确定我的代码在我的应用程序中调用了正确的名称。 – HurkNburkS