为什么我的自定义委托不工作?

发布于 2024-12-03 16:09:30 字数 1642 浏览 1 评论 0原文

我正在尝试构建一个委托,以允许我将数据从子视图控制器传递给父视图控制器。我一直在网上查看各种教程/问题,但我的委托方法没有被触发。

你能看一下我下面的代码,看看我是否遗漏了什么吗?

TownListViewController.h

@protocol TownListViewControllerDelegate;

@interface TownListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {

    id <TownListViewControllerDelegate> delegate;

}

@property (nonatomic, assign) id <TownListViewControllerDelegate> delegate;

@end

@protocol TownListViewControllerDelegate
@optional

- (void)didSelectTown:(Town *)town;

@end

TownListViewController.m

@implementation TownListViewController

@synthesize delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
    [self.navigationController popViewControllerAnimated:YES];
}

@end

SearchViewController.h

@interface SearchViewController : UITableViewController <TownListViewControllerDelegate> {

...

}

SearchViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TownListViewController * townViewController = [[TownListViewController alloc] initWithNibName:@"TownListViewController" bundle:nil];
    townViewController.delegate = self;
    [self.navigationController pushViewController:townViewController animated:YES];
}

- (void)didSelectTown:(Town *)town
{
    NSLog(@"didSelectTown fired");
    self.selectedTown = town;
}

非常感谢任何帮助。

I'm trying to build a delegate to allow me to pass data form a child view controller to the parent. I've been looking at various tutorials / questions online but my delegate method isn't being triggered.

Could you take a look at my code below and see if I'm missing anything?

TownListViewController.h

@protocol TownListViewControllerDelegate;

@interface TownListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {

    id <TownListViewControllerDelegate> delegate;

}

@property (nonatomic, assign) id <TownListViewControllerDelegate> delegate;

@end

@protocol TownListViewControllerDelegate
@optional

- (void)didSelectTown:(Town *)town;

@end

TownListViewController.m

@implementation TownListViewController

@synthesize delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
    [self.navigationController popViewControllerAnimated:YES];
}

@end

SearchViewController.h

@interface SearchViewController : UITableViewController <TownListViewControllerDelegate> {

...

}

SearchViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TownListViewController * townViewController = [[TownListViewController alloc] initWithNibName:@"TownListViewController" bundle:nil];
    townViewController.delegate = self;
    [self.navigationController pushViewController:townViewController animated:YES];
}

- (void)didSelectTown:(Town *)town
{
    NSLog(@"didSelectTown fired");
    self.selectedTown = town;
}

Any help is much appreciated.

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

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

发布评论

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

评论(1

月棠 2024-12-10 16:09:30

设置委托后尝试重新加载数据。我认为对表的 reloadData 的第一次调用发生在 TownListViewController 的 initWithNibName 期间...无论如何,您可以在此方法上发布 delegate 值吗:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

我打赌是 nil...

顺便说一下,这是一个很好的方法练习检查变量 delegate 是否符合协议,尝试在假设该方法是可选的情况下添加此行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([delegate conformsToProtocol(TownListViewControllerDelegate)] && [delegate respondsToSelector:@selector(didSelectTown:objectAtIndexPath:)])
        [delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
    [self.navigationController popViewControllerAnimated:YES];
}

try to reloadData after set the delegate. I think the first call to reloadData of table occurs during the initWithNibName of the TownListViewController... Anyway can you post the delegate value on this method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

I bet is nil...

By the way is a good practice to check if the variable delegate conforms the protocol, try adding this line under supposing that the method is optional:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([delegate conformsToProtocol(TownListViewControllerDelegate)] && [delegate respondsToSelector:@selector(didSelectTown:objectAtIndexPath:)])
        [delegate didSelectTown:(Town *)[self.fetchedResultsController objectAtIndexPath:indexPath]];
    [self.navigationController popViewControllerAnimated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文