How can I access a DetailViewController method from SourceViewController; both inside a NSSplitViewController (Objective-C)

57 Views Asked by At

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

1

There are 1 best solutions below

0
Serge Patrice Lonla On

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 ?