为什么我的委托方法没有被调用?

发布于 2024-12-21 07:44:55 字数 1197 浏览 0 评论 0原文

在此代码中,我尝试使用协议将数组从一个选项卡视图传递到另一个选项卡视图。该方法本身是一个简单的 get 方法,其中一行返回一个 mutableArray。在它自己的类中它可以工作,在这个类中它甚至没有被调用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]];
}

接收数据的类的头文件:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol CoreDataDelegate;

@interface ListTableViewController : UITableViewController
{
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *myLocationEntityArray;

    id <CoreDataDelegate> delegate;
}

- (NSMutableArray *)fetchCoreData;

@property(nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property(nonatomic, assign) id <CoreDataDelegate> delegate;
@property(nonatomic, retain) NSMutableArray *myLocationEntityArray;

@end

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (NSMutableArray *) getMyLocationEntityArray;

@end

发送数据的头文件的顶部:

@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate>

In this code I'm attempting to pass an array from one tab view to another using protocols. The method itself is a simple get method with one line returning an a mutableArray. Within it's own class it works, within this class it is not even called.

- (void)viewDidLoad
{
    [super viewDidLoad];
    myLocationEntityArray = [[NSMutableArray alloc] initWithArray:[[self delegate] getMyLocationEntityArray]];
}

The header file for the class receiving the data:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol CoreDataDelegate;

@interface ListTableViewController : UITableViewController
{
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *myLocationEntityArray;

    id <CoreDataDelegate> delegate;
}

- (NSMutableArray *)fetchCoreData;

@property(nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property(nonatomic, assign) id <CoreDataDelegate> delegate;
@property(nonatomic, retain) NSMutableArray *myLocationEntityArray;

@end

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (NSMutableArray *) getMyLocationEntityArray;

@end

The top of the header file sending the data:

@interface MapViewController : UIViewController <CLLocationManagerDelegate, CoreDataDelegate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拧巴小姐 2024-12-28 07:44:55

首先,您应该像这样更改您的协议:

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray;

@end

您将 MapViewController 设置为响应您的协议 CoreDateDelegate。因此,我假设您在 MapViewController 内分配了 ListTableViewController。如果是这种情况,您需要执行以下操作:

// MapViewController.m
...
ListTableViewController *listVC = [[ListTableViewController alloc] init];
listVC.delegate = self;
// display your listVC
...

// Somewhere in your code of MapViewController.m
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray {
   // do something with entityArray
}

编辑

根据您的评论,这里有一个更简单的方法来执行您想要的操作。 NSNotification。它不需要协议,并且更容易实现。

// In ListTableViewController.m
// In Init or viewDidLoad function


   [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getEntity:) 
                                                 name:@"GetEntity"
                                               object:nil];

- (void)getEntity:(NSNotification *)notif {
   NSArray *entityArray = (NSArray *)[notif object];
   // do something with entityArray
}

// In dealloc or viewDidUnLoad function
   [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:@"GetEntity"
                                               object:nil];


// In MapViewController.m
// theEntityArray to be defined
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GetEntity"
                                                         object:theEntityArray 
                                                       userInfo:nil];

简而言之,当您在 MapViewController 中发布 GetEntity 通知时,它会间接调用 ListTableViewController-(void)getEntity: 函数

First you should change your procotol like this :

@protocol CoreDataDelegate

//- (NSMutableArray *) fetchCoreData;
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray;

@end

You set your MapViewController to responds to your protocol CoreDateDelegate. So, I suppose you alloc your ListTableViewController inside the MapViewController. If that is the case, you need to do this :

// MapViewController.m
...
ListTableViewController *listVC = [[ListTableViewController alloc] init];
listVC.delegate = self;
// display your listVC
...

// Somewhere in your code of MapViewController.m
- (void) getMyLocationEntityArray:(NSMutableArray *)entityArray {
   // do something with entityArray
}

EDIT

Following your comments, here is a simpler way to do what you want. NSNotification. It does not require protocol, and is easier to implement.

// In ListTableViewController.m
// In Init or viewDidLoad function


   [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(getEntity:) 
                                                 name:@"GetEntity"
                                               object:nil];

- (void)getEntity:(NSNotification *)notif {
   NSArray *entityArray = (NSArray *)[notif object];
   // do something with entityArray
}

// In dealloc or viewDidUnLoad function
   [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                 name:@"GetEntity"
                                               object:nil];


// In MapViewController.m
// theEntityArray to be defined
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GetEntity"
                                                         object:theEntityArray 
                                                       userInfo:nil];

In few words, when you will post the GetEntity notification in MapViewController, it will call undirectly the -(void)getEntity: function of ListTableViewController

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文