【发布时间】:2020-12-07 14:25:57
【问题描述】:
我是 Objective-C 的新手,觉得学习很重要。我给自己提出了以下挑战:
我有一个 NSSplitViewController 设置,每个视图有两个类; (i) SourceViewController: NSViewController 和 (ii) DetailViewController: NSViewController。
通过选择 SourceViewController 的 NSTableView 行,我想更改 SourceViewController 内 ImageView 中的图像
我在 DetailViewController 中有一个实例类方法,我试图从 SourceViewController 访问它:
-(void) imageSelected: (NSString*) name{
self.imageView.image = [NSImage imageNamed:name];
}
为了获得访问权限,我通过 NSSplitViewController 超类创建了一个指向 DetailViewController 类实例的指针。所以我上树再下到 DetailViewController:
//gets the row number that was selected
-(void) tableViewSelectionDidChange:(NSNotification *)notification{
NSLog(@"%ld",[[notification object] selectedRow]);
NSInteger row = [[notification object] selectedRow];
//get access to the parent view controller
//NSSplitViewController *splitCV = self.parentViewController;
NSSplitViewController *splitCV = ((NSSplitViewController *)self.parentViewController);
NSViewController *imageVC = splitCV.childViewControllers[1];
NSLog(@"%@",imageVC.className); //detail view controller
//***** HERE'S THE PROBLEM******
imageVC.imageSelected(self.pictures[row]); //<- imageSelect isn't recognised
}
但是,这不起作用,因为自动完成无法识别 imageVC 指向的函数。打印 imageVC.className 输出,显示名称是 'DetailViewController'。
我做错了什么,我该如何解决?
提前致谢
【问题讨论】:
-
问题是为什么
imageVC.imageSelect(self.pictures[row]);无法编译?此行是从 Swift 代码中复制的吗? -
嗨 - 这是 Objective-C 代码;相当于 [imageVC imageSelect: self.pictures[row]]。问题是我做错了,因为自动更正无法识别“imageSelect”功能
-
@awyr_agored 您的函数名称是 imageSelected 但在 tableViewSelectionDidChange 中您调用了一个名为 imageSelect 的函数。函数名称必须匹配。另外,用对 imageSelect 的调用中包含哪些图片以及 self.pictures[row] 应该代表什么来更新您的问题。
标签: objective-c xcode macos cocoa nssplitviewcontroller