使用委托方法访问另一个类中定义的函数

发布于 2024-12-02 13:21:19 字数 1251 浏览 8 评论 0原文

- (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 技术交流群。

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

发布评论

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

评论(1

昔日梦未散 2024-12-09 13:21:19

问题如下:方法[[UIApplication sharedApplication] delegate];返回类CortesAppDelegate的对象。因此,您无法调用 showAddress,因为它是在 CortesViewController 中定义的

The problem is in following: method [[UIApplication sharedApplication] delegate]; returns object of class CortesAppDelegate. So you cannot call showAddress as it is defined in CortesViewController

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