【问题标题】:protocol and delegate not accessing method协议和委托不访问方法
【发布时间】:2013-05-16 01:26:59
【问题描述】:

我正在尝试将一些数据传递回我的 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

这就是我的代码设置协议和委托的样子

ma​​inViewController.h

#import "SeriesDataClass.h"

@interface MatchingSeriesViewController : UIViewController <GetSeriesViewParsedData>
{

ma​​inViewController.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];
//..

ma​​inViewController.m

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

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

所以我的问题是我在设置此协议/委托时缺少什么以使其无法正常工作?

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

【问题讨论】:

    标签: ios uiviewcontroller delegates protocols nsobjectcontroller


    【解决方案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];
    

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

    【讨论】:

    • 你有关于如何做到这一点的任何线索我什至不知道在谷歌上搜索什么与我遇到的问题相同。
    • 好的很酷,慢慢阅读它然后尝试实现它我可能需要 15 分钟:P 感谢您的编辑:P
    【解决方案2】:

    改变这个:

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

    【讨论】:

    • 好的,我已经改变了它,但它仍然没有得到那个方法。
    • @HurkNburkS,你为什么用 GetSeriesResultItem 分配一个 SeriesDataClass 对象?然后在你的下一行你有 [SeriesResultItem setDelegate:self]。什么是 SeriesResultItem?
    • 对不起 seriesResultClass 是我写这个问题的旧名称.. 然后决定尝试一些我创建了一个名为 seriesDataClass 的新类,我忘了用它更新问题代码.. 忽略SeriesResultItem 我现在将更改它.. 但我 100% 确定我的代码在我的应用程序中调用了正确的名称。
    • 我已经更新了问题中的代码并仔细检查了我的代码是否调用了正确的类/方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多