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

时间:2011-09-02 13:15:11

标签: iphone uiapplicationdelegate

- (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', reason: '-[CortesAppDelegate showAddress]: unrecognized selector sent to instance 0x146ce0'"

在第

returnToMapDelegate = (CortesViewController *)[[UIApplication sharedApplication] delegate];

我在代码中成功使用了委托方法。

CortesAppDelegate *appDelegate = (CortesAppDelegate *)[[UIApplication sharedApplication] delegate];
      [appDelegate somefunction] ;

但是当我为CortesViewController尝试相同的事情时,应用程序崩溃了。我是iOS新手。我可能错过了非常简单的观点。

有人能告诉我代码有什么问题吗?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

问题在于:方法[[UIApplication sharedApplication] delegate];返回类CortesAppDelegate的对象。因此,您无法调用showAddress

中定义的CortesViewController