1

I have an app that should display a Youtube video and I'm using the Youtube ios helper API.

I have a subclass of UITabBarController that is used to present two youtube videos and the UITabBarController is inside a NavController and is pushed to from a UITableViewController. So the setup looks like

[NAVController] -- relationship --> [UITableViewController] -- push --> [UITabBarController] -- relationship --> [CustomViewController]

Now my CustomViewController.h looks like:

#import <UIKit/UIKit.h>
#import "YTPlayerView.h"

@interface CustomViewController : UIViewController<YTPlayerViewDelegate>

@property (strong, nonatomic) YTPlayerView *youtubeView;
@property (strong, nonatomic) NSString *videoID;

@end

and my .m file has

#import "CustomViewController.h"
#import "YTPlayerView.h"

@interface CustomViewController ()

@end

@implementation CustomViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.youtubeView.delegate = self;

    self.view.backgroundColor = [UIColor blueColor];

    self.youtubeView.frame = self.view.frame;

    self.youtubeView.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.youtubeView];

    NSLog(@"Subviews %lu", (unsigned long)[self.view.subviews count]);
    NSLog(@"Subviews %@", self.views.subviews);

    BOOL b = [self.youtubeView loadWithVideoId:self.videoID];
    NSLog(@"Loaded? %d", b);
    [self.youtubeView playVideo];        
}

What I end up with is a navigation bar at the top (as it should be), a tab bar at the bottom (as it should be), but with a blue background instead of a red background (and needless to say, no Youtube video loads). When I run this, it prints

Subviews 0 Subviews ( ) Loaded? 0

Clearly the addSubView: call is not working.

I've seen this post, but it didn't help.

(For those unfamiliar with the youtube ios helper library:

The youtube-ios-player-helper is an open source library that helps you embed a YouTube iframe player into an iOS application. The library creates a UIWebView and a bridge between your application’s Objective-C code and the YouTube player’s JavaScript code, thereby allowing the iOS application to control the YouTube player.

(Using iOS 7 and Xcode 5.1)

Community
  • 1
  • 1
marisbest2
  • 1,346
  • 2
  • 17
  • 30
  • 1
    Where are you initializing: `self.youtubeView` ? aka: `self.youtubeView =[YTPlayerView alloc]init];` – klcjr89 Jul 16 '14 at 20:00
  • Weird. That didn't work for me before, but now seems to... Thanks! As a followup, in the YT docs, it doesn't show initialization. Is an IBOutlet automatically alloc'd and initialized? – marisbest2 Jul 16 '14 at 20:09
  • Yes, an IBOutlet is automatic – klcjr89 Jul 16 '14 at 20:10

2 Answers2

3

Since I don't see that you're using an IBOutlet, I believe you need to initialize the self.youtubeView:

self.youtubeView = [[YTPlayerView alloc]init];

self.youtubeView.delegate = self;

self.view.backgroundColor = [UIColor blueColor];

self.youtubeView.frame = self.view.frame;

self.youtubeView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.youtubeView];
klcjr89
  • 5,862
  • 10
  • 58
  • 91
0

The YouTube api isn't the problem here, UINavigation controller and UITabBarController just dont play nicely together. What you should do instead is make a view controller to handle the tabbar instead of a UITabBarController. This shows some insight on how to get it done.

Community
  • 1
  • 1
David Cao
  • 525
  • 3
  • 11
  • Haha, it seems that troop231's answer solved it for you, but if you have troubles with the frames this should help – David Cao Jul 16 '14 at 20:19
  • They seem to be playing nicely for me. What doesnt work well? – marisbest2 Jul 16 '14 at 20:29
  • Sorry, I meant pushing views. I had trouble with it in my previous apps, take a look at http://stackoverflow.com/questions/576764/tab-bar-controller-inside-a-navigation-controller-or-sharing-a-navigation-root It might have been me just doing something weird, though :P – David Cao Jul 16 '14 at 20:39
  • I used Storyboard segues and it works fine. I'm surprised it doesnt work using pushViewController:animated:. iOS often has that weirdness – marisbest2 Jul 17 '14 at 14:25