为什么我的自定义委托不工作?
我正在尝试构建一个委托,以允许我将数据从子视图控制器传递给父视图控制器。我一直在网上查看各种教程/问题,但我的委托方法没有被触发。
你能看一下我下面的代码,看看我是否遗漏了什么吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置委托后尝试重新加载数据。我认为对表的 reloadData 的第一次调用发生在 TownListViewController 的 initWithNibName 期间...无论如何,您可以在此方法上发布
delegate
值吗:我打赌是 nil...
顺便说一下,这是一个很好的方法练习检查变量
delegate
是否符合协议,尝试在假设该方法是可选的情况下添加此行: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: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: