Access Parent View Controller from tableViewController in popover view - objective-c

I am trying to implement a popover view in a tableview controller. My intention is for the user to select an option from the table list as shown below.
Note that my popover view actually displays the data from a separate table view controller. I am creating the popover view controller via the following initialization method
self.popOverViewController = [[UIPopoverController alloc]initWithContentViewController:optionsTableViewController];
After the user selects an option for example "Hottest All Time", the control should be passed from the tableview Controller (in the popover view) back to the MAIN table view controller (parent view) so as to trigger a table reloadData method.
Query: Is there a way to return the control from the tableview controller in the popover controller back to the MAIN tableview controller?
Do I have to use a delegate method to do this?

The two approaches I've seen are roughly the standard sort of fare:
create a delegate protocol for the class type of optionsTableViewController, have the controller that creates the popover implement it and set itself as the delegate when issuing the popover
use the NSNotificationCenter (which actually fits the intended purpose of the thing if you've a one-to-many message, as may be the case if you've a popover with a setting that affects a bunch of different controllers and you don't really care which is visible when the user requests the popover)

Related

Dismiss popover from a table selection

I'm sorry if this duplicates other threads. I've pored through about a dozen, over several hours, but none seem to quite apply to my situation. Namely;
A button presents a popover
The popover contains a table view, nested inside a navigation controller
The user navigates to the second level of the nav controller (a second tableViewController), then makes a selection
Upon making the selection, the popover should dismiss, and pass back the indexPath.row to the original screen.
Importantly, I'm using storyboards and segues to do this (this may be part of the problem!)
I've tried implementing custom delegate methods to do this, but I'm getting hopelessly tangled up. Mainly because
a) The actual delegate is two levels away, and I'm having trouble conveying this "up the chain", as it were.
b) The [segue destinationViewController] is the navigationController. I'm not sure how to get a hook into the actual tableViews it contains, to retrieve or set properties (such as the delegate)
Does this make any sense to anyone? Reading back, this question is almost as bamboozled as I am. If you can decipher it, and have any advice, I'd be very grateful.
You can get to the actual view controller (which has your table view) using the viewControllers property of your navigation controller (segue.destinationViewController). Once you have a pointer to this view controller, set its delegate. Then in tableView:didSelectRowAtIndexPath, notify the delegate that something was selected, and the delegate can dismiss the popover.
EDIT: This could be in your prepareForSegue:
UINavigationController *navigationController = (UINavigationController *)segue.destinationViewController; // cast the destination to UINavigationController
SpeciesTableViewController *speciesViewController = [navigationController.viewControllers lastObject];
speciesViewController.delegate = self;
Apple docs about the viewControllers property of a UINavigationController:
The view controllers currently on the navigation stack. . . . The root
view controller is at index 0 in the array, the back view controller
is at index n-2, and the top controller is at index n-1, where n is
the number of items in the array.
When using a segue, the root view controller is the only view controller, so lastObject always returns the root view controller.
Now, keep in mind that when you select a species in SpeciesTableViewController, you're triggering a segue, and will have to set the delegate of SpeciesDetailViewController. In SpeciesDetailViewController's didSelectRowForIndexPath you can send a message to the delegate to dismiss the popover.

re-adding a view to the view controller from another class

I add a UIView, minimisedInterface, to my view controller as normal
[self.view addSubview:minimisedInterface];
minimisedInterface is removed from the view controller, on a button tap.
[self removeFromSuperview];
I now want to re-add the minimisedInterface to the view controller, but on a button tap in another class, Interface.
What is the correct way to do this? I was considering getting a handle on the view controller from the Interface class but this seems a little counter-intuitive, as if one shouldn't "control" the view controller itself.
Thanks in advance :)
The two likeliest ways that come to mind are:
make the view controller a delegate of the Interface class and have a [delegate presentView]; method (or something of the kind)
send a NSNotification that the view is needed again, from the buttons IBAction, and have the view controller listen for that notification.

Create a UIViewController that contain a UIViewController

I have a UIViewController that allow me to display some text into a view.
I want to add an input method without add it directly into this view controller, this input method will be a button or a UITextField.
This input method will be a lot, but it will be use one at time chosing it from setting so a I won't to have a UIViewController that control all of this.
What I want is know how it's possible to split the output view (controller) with each input view (controller)?
You can image to have a text view on the top of the screen and some other view at the bottom and I will to separate the logic of the second view from the logic of the first one
Is it clear?
Of course it is possible to have 2 UIViewControllers on screen at any time! There are a few ways to go about this:
Using One Main View: Add the second ViewController's view as a subview
Using Interface Builder: Drag in a UIViewController Object, set it's class, then hook it's View outlet to a UIView in the first ViewController.
Child View Controller: As it's name implies, the -addChildViewViewController: method will add a new ViewController as well, then add it's view as a subview.
Yes, it's possible. Just add the controller as a child controller ([UIViewController addChildController:] and its view ([controller.view addSubview:childController.view]).

Add other objects to iOS 5 storyboard after tableview

I have a simple iOS 5 storyboard that contains a tableview controller scene. However, the table UI takes up 100% of the real estate and I am unable to add any additional objects such as a title bar. Any object that I drag to the scene will try to size correctly and what-not but as soon as I let go it will not add the object. What am I doing wrong?
If all you want is a title bar, then it looks like you want to embed your table view controller in a navigation controller. You can do this by selecting your table view controller and using the Editor: Embed In: Navigation Controller menu command. Once you do this, you should have a navigation bar, and you can double click it to edit the title.
If you need arbitrary UI elements along with your table view, then I think you need to use a plain UIViewController scene instead of a UITableViewController, and manually drag a UITableView into the scene. Your view controller would not subclass UITableViewController, instead it would subclass UIViewController and implement the UITableViewControllerDelegate and UITableViewControllerDataSource protocols. Also, you would need to manually wire up the delegate and dataSource outlets by ctrl-dragging from the table view to your view controller in interface builder, and your view controller would need a custom tableView outlet that points to the UITableView and is correctly wired up in IB. Perhaps there is a simpler approach than this though, if someone has a better suggestion that would be great to hear.

Objective-C: How to send signal from view to view controller to change views?

I have a view controller that controls the switching between views. I would like one of the views to signal the view controller to switch to another view (and can't figure out how I can do this.)
To be more clear (hopefully): My view controller inserts a subview. That subview has a UITableView. I'd like, when you select a row in the UITableView, to remove the current subview and then switch to a different sub-view. Of course, I'd prefer the view controller to continue to keep track of which subview is loaded.
Does this make sense? (I'm still pretty green with Objective-C.)
Is there a way to send the view controller a message from the sub-view (that the view controller created)? Is there another way to accomplish this?
Thanks a bunch for the help... and I'd be happy to clarify if needed.
You might look into setting up a UINavigationController. Use the 2 UIViewControllers to control the individual views, and use the Navigation Controller to switch between the 2 views. From the UITableView, you can simply implement the method -
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Use this method to alloc the new view controller that you want to display
Then call the Navigation controller to push the new view controller onto the stack -
[self.navigationController pushViewController:controllerName animated:YES]
Finally, release the view controller that has disappeared.
This way the navigation controller keeps track of who is loaded, and can implement convenience functions like animating the transition. Also make sure to lookup the UITableViewController subclass - it is a subclass of UIViewController, but it provides some convenience functions for dealing with tables, like knowing when the user selects a particular row, and allows for the standard edit functions of most iOS apps.