I have a view controller with an image view in it.
I have a popover with a table view in it which is anchored to a bar button in this view controller.
I would like to be able to load images into the image view by using the table in the popover.
Both the popover and the main view controller have separate view controller classes.
I have launched the popover from a segue.
How can I do this?
I am assuming that your segue takes you from your imageViewController to your popped-over tableViewController.
Then you can set your imageViewController as delegate to the tableViewController, so that you can call methods on it from the tableViewController in a decoupled manner.
MyTableViewController.h
In your tableViewController header file declare a protocol which it will expect it’s delegate to follow. Place it above your @interface section:
Also declare a property to hold a reference to it’s delegate:
The protocol declares the method signature that your tableView will expect to be able to call on its delegate. It allows it to send back some data, and get itself dismissed. The delegate (in this case, your imageViewController) will have to implement this method.
MyTableViewController.m
The method is called on the delegate when a table cell is selected:
MyImageViewController.h
include MyTableViewController.h and add the delegate protocol to the
@interface.Declare a property to hold a reference to your UIPopOverController so that you can send it a dismiss message:
(these steps could be moved to your .m file’s category extension for better encapsulation).
MyImageViewController.m
You will set the delegate property in
MyImageViewController‘sprepareForSeguemethod, which gets called when the segue is invoked.You will also set the reference to the popoverController here.Lastly, you implement the tableViewController’s delegate method:
update
Aside from the fact that the popOverController itself is a slightly unusual entity (a controller without a view, inheriting directly from NSObject), most of this is the standard delegation pattern. You could simplify it somewhat by using a bit of indirection and runtime checking in
didSelectRowAtIndexPath:In this case you would not need to define the protocol or
<adhere>to it, and you wouldn’t need to#import MyTableViewController. However the compiler would give you no help if you did not implement the method correctly. Which, as you can see from my earlier mistake, is probably unwise.