0

I'm new to Objective-C and feel it's important to learn. I gave myself the following challenge:

I have a NSSplitViewController setup with two classes for each view; (i) SourceViewController: NSViewController and (ii) DetailViewController: NSViewController.

By selecting a NSTableView row of the SourceViewController I want to change the image in the ImageView within the SourceViewController

enter image description here

I have an instance class method in DetailViewController that I'm trying to access from SourceViewController:

-(void) imageSelected: (NSString*) name{
    self.imageView.image = [NSImage imageNamed:name];
}

To gain access, I have created a pointer to the DetailViewController class instance via the NSSplitViewController superclass. So I go up the tree and down again to the 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
}

However, this doesn't work as autocomplete doesn't recognise the function pointed to by imageVC. printing the imageVC.className output, shows that the name is 'DetailViewController'.

What am I doing wrong and how can I fix this?

Thanks in advance

awyr_agored
  • 613
  • 3
  • 19
  • Is the question why `imageVC.imageSelect(self.pictures[row]);` doesn't compile? Is this line copied from Swift code? – Willeke Aug 18 '20 at 13:29
  • Hi - it's Objective-C code; equivalent to [imageVC imageSelect: self.pictures[row]]. The problem is I'm doing something wrong since autocorrect does not recognise the 'imageSelect' function – awyr_agored Aug 18 '20 at 14:55
  • @awyr_agored Your function name is imageSelected but in tableViewSelectionDidChange you call a function called imageSelect. The function names must match. Also, update your question with what pictures is in the call to imageSelect and what self.pictures[row] is supposed to represent. – Swift Dev Journal Aug 18 '20 at 20:00
  • See https://stackoverflow.com/search?q=xcode+autocomplete+not+working. – Willeke Aug 19 '20 at 05:18

1 Answers1

0

Can you try changing this:

NSViewController *imageVC = splitCV.childViewControllers[1];

into this:

DetailViewController *imageVC = splitCV.childViewControllers[1];

and see if imageVC.imageSelected(self.pictures[row]) now autocompletes correctly ?