使用委托方法访问另一个类中定义的函数
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"didselectrowatindexatpath");
Place *placeAtIndex = (Place *)[appDelegate.PlacesArray objectAtIndex:indexPath.row];
NSLog(@"required lat long is %f, %f ", placeAtIndex.PlaceLatitude, placeAtIndex.PlaceLongitude);
returnToMapDelegate = (CortesViewController *)[[UIApplication sharedApplication] delegate];
[returnToMapDelegate showAddress];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is Called in PlacesViewController.m and Function "showaddress" is defined in CortesViewController.h.
时收到错误“由于未捕获的异常
'NSInvalidArgumentException' 而终止应用程序,原因:'-[CortesAppDelegate showAddress]:无法识别的选择器发送到实例 0x146ce0'”
我在
returnToMapDelegate = (CortesViewController *)[[UIApplication sharedApplication] delegate];
代码中成功使用以下委托方法
CortesAppDelegate *appDelegate = (CortesAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate somefunction] ;
。但是当我对 CortesViewController 尝试同样的操作时,应用程序崩溃了。我是 iOS 新手。我可能错过了非常简单的一点。
谁能告诉我代码有什么问题吗?
感谢您提前的帮助
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"didselectrowatindexatpath");
Place *placeAtIndex = (Place *)[appDelegate.PlacesArray objectAtIndex:indexPath.row];
NSLog(@"required lat long is %f, %f ", placeAtIndex.PlaceLatitude, placeAtIndex.PlaceLongitude);
returnToMapDelegate = (CortesViewController *)[[UIApplication sharedApplication] delegate];
[returnToMapDelegate showAddress];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is Called in PlacesViewController.m and Function "showaddress" is defined in CortesViewController.h.
I am getting error "Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[CortesAppDelegate showAddress]: unrecognized selector sent to instance 0x146ce0'"
at line
returnToMapDelegate = (CortesViewController *)[[UIApplication sharedApplication] delegate];
I used successfully following delegate method in code.
CortesAppDelegate *appDelegate = (CortesAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate somefunction] ;
But when I tried same thing for CortesViewController, application crashes. I am new to iOS. It might be possible that I am missing very simple point.
Can anyone tell me what is wrong with the code?
Thanks for help in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题如下:方法
[[UIApplication sharedApplication] delegate];
返回类CortesAppDelegate
的对象。因此,您无法调用showAddress
,因为它是在CortesViewController
中定义的The problem is in following: method
[[UIApplication sharedApplication] delegate];
returns object of classCortesAppDelegate
. So you cannot callshowAddress
as it is defined inCortesViewController