Accessing UIViewController from UITableViewController? - iphone

In iOS4, I want to use MPMoviePlayerController. I have a UIViewController that I pass to a custom class that manages MPMoviePlayerController. That plays video fine.
I have another view that is a UITableViewController. Passing the UITableViewController to my UIViewController property on the video class doesn't work. I just get sound. I'm fairly sure this is because the UITableViewController needs to be a UIViewController. I can change the UITableViewController to a UIViewController but this is a fairly good rewrite. Is there a way to access the parent UIViewController behind the UITableViewController?
I have also tried
(UIViewController*)self;
but that doesn't work either. I just get sound again.

You can use `self.parentViewController. From the documentation:
Parent view controllers are relevant
in navigation, tab bar, and modal view
controller hierarchies. In each of
these hierarchies, the parent is the
object responsible for displaying the
current view controller. If you are
using a view controller as a
standalone object—that is, not as part
of a view controller hierarchy—the
value in this property is nil.

Related

UIView embedded only in UINavigationController and UITabBarController, why?

In iOS, why can a UIView (and others) be only embedded in a UINavigationController or UITabBarController? What's special about these two classes?
Edit: oops, wanted to ask 'UIViewController' instead of UIView.
No, its not true. UIView is embedded with UIViewController. This class provides life cycle for UIView. It takes responsibility from initializing the view to deallocating the view.
UINavigationController and UITabBarController are just derived from UIViewController. They provide extra functionality for building hierarchy and switching between hierarchy respectively.
UIView can be embedded in any ViewController or its subclass. UINavigationController and UITabbarController are nothing but subclasses of UIViewController.
I think the embedding you're referring to is the embed in menu item which only allows for UITabBarController and UINavigationController. This means that XCode will take your UIViewController subclass and embed it in one of these two controllers. They are special because they are controllers of other controllers (collections of UIViewControllers). Xcode is simply taking some of the pain out of building a view controller and then adding it to a navigation controller or a tab bar. You can easily embed it in one of these with one click and no code. Much easier than in past versions of XCode.
If you're talking about "embed" as in Interface Builder, yes, as of iOS 5, Interface Builder only gracefully designs user interfaces for three view controller containers, UINavigationController, UITabbarController, and UISplitViewController. These are the three container controllers that come out of the box. You can, though, do your own view controller containment. See Session 102 in WWDC 2011 for information on view controller containment. Also refer to the section on view controller containment in the UIViewController Class Reference.

Using delegate with tabBarController on Storyboard

Would appreciate any advice as I try and wrap my head around this - I'm not sure if I'm implementing this wrong, or am working from the wrong premise (or both).
If I have a class in which I've created a protocol (the delegatOR) - in order to assign a delegate for that protocol, am I right to say I need to alloc/init the 'delegatee' class somewhere in the delegator's implementation file, and then assign it as the delegator's delegate?
If so, follow up question: I have a tabBarController set up in Storyboard, and when the user clicks on the 'end' tab I'd like to send a message to the viewController for the view they're about to leave, so it pops up an alert saying something like 'are you sure?'. Since storyboard does the initializing and allocating behind the scenes, I'm at a loss as to how to set up the delegate. I read in another posting about using the prepareForSegue method when segueing between two viewControllers to set the delegate, but can't work out a similar catch-and-set technique for the tabBarController.
If I have a class in which I've created a protocol (the delegatOR) - in order to assign a delegate for that protocol, am I right to say I need to alloc/init the 'delegatee' class somewhere in the delegator's implementation file, and then assign it as the delegator's delegate?
No. The way it usually works is that some other class, usually some kind of controller, will instantiate (i.e. alloc/init) an object as well as its delegate, and set the object's delegate.
For example, if you have a UITableView and a UITableViewDelegate, typically you have some kind of view controller that owns the UITableView, and also owns the UITableViewDelegate (often the view controller itself is the delegate). It's the view controller's job to set the table view's delegate. It most certainly is not the UITableView's job to create and set its delegate.
Still, you're right that setting delegates for view controllers can be hard when you're using Storyboard. If the prepareForSegue: method isn't working for you, I assume it's because the tab bar controller is your root view controller (as it well should be, according to the HIG). Since your app delegate will have a window property, you should be able to get to the root view controller from it:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
tabController.delegate = self;

Trouble presenting Modal View Controller (MPMoviePlayerViewController)

My App starts up, and I have a UITabBarController. On the first tab I have a subclass of UIViewController. It's in this class that I have my movie playing functionality. I do not know why, but from this class, I cannot presentModalViewController or presentMoviePlayerViewControllerAnimated. I can however add views as a subView. I found this post, which is essentially what I'm trying to do (present a movie player view controller): How to present MPMoviePlayerViewController from a UITabBarController?
But even keeping a reference to the UITabBarController does not work for me. I'm not really sure why this class has problems presenting a modal view controller, versus in other tabs, I am able to. Any thoughts? Thanks.
After more looking, I found that the problem is I cannot present a view modally within the viewDidLoad method.

Subclassing UITableViewController for PopoverController

I have a ViewController that is composed of a few different views on my screen. A scrollView for text data, a TableView for some other data, etc. In my app, I want to add a UIPopOverController to show a list of my data. The current ViewController I am in is not a subclass of UITableViewController. Do I have to create a separate subclass of UITableViewController in another file, and use an instance of that class in this ViewController? Thanks.
You can show any kind of UIViewController in a UIPopoverController. A table view is not at all required for displaying a Popover controller. If you want to display a UITableviewController, you most certainly can. Just pass it in to the popover controller.

Design view in Interface Builder to load lazily in UIViewController

I've managed to get my myself confused... I've got a fairly complex View Controller much of which is developed programatically. I'd like to pop up a "dialog" on top of the view controller at some point and I'd like to "design" that view in Interface Builder because it's fairly straightforward (background UIImageView, some UILabels and a UIButton).
I've created the .xib and am now subclassing UIView with the IBOutlets,etc. I'm now trying to wire it up and realizing I probably need to add an initWithNibName: method so this will instantiate correctly...then I realize that I'm really just making another UIViewController and I don't think we're supposed to have UIViewController views w/in other UIViewController views?!?
So now I'm re-thinking how to go about this "correctly." How best to use IB to design a simple 1/4 screen view that will pop up over a main view?
Call +[NSBundle loadNibNamed:owner:] to load the NIB file that contains your view. If you specify your view controller (i.e., self) as the owner, any connections you make to File's Owner in the NIB file will then be made to the view controller. So you could declare an outlet to your custom view in the view controller and after the call
[NSBundle loadNibNamed:#"MyView" owner:self];
the outlet variable will point to the view object. Alternatively, you can use -[NSBundle loadNibNamed:owner:options:], which returns an array of the top-level objects in the NIB.