Posts in uiviewcontroller

  • Don't Reference View Controller Views in init

    This is more of a note to my future self:

    Don't reference a view controller's view in an init method.

    Why not? Well, let me tell you what I did.

    The Setup

    Custom UITableViewController subclass. Has a few custom variables that
    store useful data. An array of things to display, some basic text, etc. These
    all get set in a custom init method: initWithTitle:Items:. All very simple.
    These variables then get used in viewDidLoad to setup the table view, etc.

    The Problem

    {% highlight objc %}
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELLID"]
    {% endhighlight %}

    Running that in my init function results in viewDidLoad running before I had
    set up the other variables. Which meant viewDidLoad was running before any of
    its variables were initialized correctly. Needless to say, this resulted in
    some strange behavior.

    The Solution

    Stop referencing the view in the init method.

  • MPMoviePlayerController Going Fullscreen Creates A Blackout

    The Problem:

    (Solution found via StackOverflow)

    You've added an MPMoviePlayerController to one of your view controller's
    child views. You hit play, it starts playing the video. Then you hit the
    fullscreen button. It fills the screen, and suddenly everything goes black, you
    can't get any controls, and all that happens is the audio keeps playing.

    So what went wrong?

    What Happened

    So my best guess is that the view that contains the MPMoviePlayerController
    view is a temporary one in your view controller. Something that you get rid of
    when the view controller's view stops being displayed. So what you decided was
    to remove the view from the view controller (removeFromSuperview) when the
    view controller is no long displayed.

    The obvious place to do that is in viewWillDisappear:, right? It
    consolidates all these actions into one place, and happens automatically.

    The problem? When the movie goes fullscreen, your view disappears and
    viewWillDisappear: is called.[1] MPMovePlayerController doesn't work well
    when its view gets removed from the view hierarchy. Whoops.

    The Solution

    I solved this by moving my view destroying code into a new method and out of
    viewWillDisappear:. In my case all the times I wanted the view to be removed
    are times when I'm triggering viewWillDisappear: manually with a call to
    pushViewController:animated: to the navigationController. So right
    above that call, I added a call to my new method.

    If you have situations where you don't control the transition that causes your
    view to be destroyed, you might want to add a BOOL to safegaurd the view from
    being destroyed if video playback is happening.

    1. You might run into this problem transitioning from iOS 5 to iOS 6. Apple fixed a bug where viewWillDisappear: wasn't being called for the fullscreen transition. So this doesn't happen in iOS 5, but does in iOS 6. ↩ī¸Ž