I am having problems setting up the YTPlayer from Youtube API programmatically and not over Storyboard as they show in their example..
This is what I have attempted to do but not luck, the video won't event load..
This is my viewDidLoad where I tried to set everything up.. Also, I almost forgot to say that I downloaded their master framework and added to my project. I changed the path as well for the .html file. I have already tried as they show with Storyboard and works but I really wanna to be able to do it programmatically.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myView = [[YTPlayerView alloc] initWithFrame:CGRectMake(10, 80, 300, 275)];
self.myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.myView];
[self.myYoutubePlayer loadWithVideoId:#"Yf5_ZQcP7y0"];
self.playBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 120, 80)];
[self.playBtn setTitle:#"Play" forState:UIControlStateNormal];
self.playBtn.backgroundColor = [UIColor redColor];
[self.playBtn addTarget:self action:#selector(playVideo:) forControlEvents:UIControlEventTouchDown];
self.stopBtn = [[UIButton alloc] initWithFrame:CGRectMake(190, 400, 120, 80)];
[self.stopBtn setTitle:#"Stop" forState:UIControlStateNormal];
self.stopBtn.backgroundColor = [UIColor redColor];
[self.stopBtn addTarget:self action:#selector(stopVideo:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.playBtn];
[self.view addSubview:self.stopBtn];
}
And the play and stop methods...
- (IBAction)playVideo:(id)sender {
[self.myYoutubePlayer playVideo];
}
- (IBAction)stopVideo:(id)sender {
[self.myYoutubePlayer stopVideo];
}
If more information is needed please ask first, I will be happy to provide it.. :) thanks!
The problem I had here was that the YTPlayerView framework didn't had properly define the iframe.html file to be loaded and use for the player. So, I fixed changing the TYPlayerView.m within the function -loadWithPlayerParams: and change whatever is there for the path of this file..
NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:#"YTPlayerView-iframe-player"
ofType:#"html"
inDirectory:#""];
This will make the job done..! :)
I think you have an error on your code.
You're calling "loadWithVideoId" on "self.myYoutubePlayer" and it should on "self.myView"
Related
So I am trying to hide a UIView but something weird is happening. After using this line of code:
ntcCircleView.hidden = YES;
The view won't disappear, but it hides behind another UIView.
This is the complete code I use:
UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
notificationButton = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:#selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];
ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2; // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];
ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:11.0]];
[ntcCircleView addSubview:ntcNumberLabel];
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
ntcCircleView.hidden = YES;
}else
{
ntcNumberLabel.text = [NSString stringWithFormat:#"%i",ntcNum];
}
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
[self.view addSubview:NtcContainer];
So ntcCircleView after hidding it, just goes behind notificationImage.
What drives me crazy, is that I use successfully the exact same code in another Views with the only difference the last line. Instead of using:
[self.view addSubview:NtcContainer];
I add the views to the navigation bar like this:
UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;
What am I missing here ?
UPDATE
I also noticed that this bug happens only when I use
[self.navigationController popViewControllerAnimated:YES];
to navigate back to the view.
You need to update these snippet:
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
to:
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];
It will solve your problem.
I figured it out. I was using a deprecated method (viewDidUnload) to release the notification observer.
Working off of other Stack Overflow answers, I have managed to come up with the code below:
(in my homescreen.m file)
UIButton *GCButton;
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
//add button
[self addGameCenterButton];
}
return self;
}
-(void)addGameCenterButton
{
GCButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[GCButton setBackgroundColor:[UIColor orangeColor]];
[GCButton setTitle: #"My Button" forState:UIControlStateNormal];
[GCButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
[GCButton.layer setBorderWidth:1.0f];
[GCButton.layer setBorderColor:[UIColor blueColor].CGColor];
//adding action programatically
[GCButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:GCButton];
}
-(void)buttonClicked:(UIButton*)sender
{
NSLog(#"you clicked on button %ld", (long)sender.tag);
}
As of right now, I see nothing on my screen. Is there anything else I need to add in my .m or .h files to make the button work?
From the comments of your question, I'm assuming you are using SpriteKit. In a GameScene, unfortunately, you cannot use UIButtons normally.
To create buttons for your scene, please check the answers in the link below:
Setting up buttons in SKScene
I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end
I am new to IOS and Objective-C , and I try to set Image for a UIButton.
Wen I click the button , the button will change image1. If I click the button one again , it will change to image2
First, I set the image in Viewdidload like the following code.
#interface AITPreviewViewController ()
{
VLCMediaPlayer *mediaPlayer ;
BOOL recording ;
BOOL recordMode;
}
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
When I click the button , the button will change the image. The code is like the following:
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
if (recordMode == YES) {
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateNormal];
recordMode = NO;
}else if (recordMode == NO){
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
recordMode = YES;
}
}
When I click the image on the button , the image of the button become dark in color. It seems the button has been press. But the - (IBAction)modeButtonClick:(id)sender didn't called.
When I click the blank space (the side of image) , the button has been press. The - (IBAction)modeButtonClick:(id)sender has been called. And the image also change to other image.
I am sure I have set action in header file and .m file.
The above describe is like the following picture.
It seems the button is cover by image , and the image and the button is not the same.
Why this happened ??
How to solve this problem ?
Thanks in advance.
Couple of changes that you could implement:
First:
Change the setBackgroundImage to setImage.
For example: [self.modeChangeButton setImage: [UIImage imageNamed: #"recordmode.png" forControlState: UIControlStateNormal];
That way, if you have set an image to the button by mistake, this will overwrite it.
Second:
Make sure you have connected your reference outlets (IBOutlet) and actions (IBActions) in Interface builder - whether XIB files, or storyboard. If you don't link the the interface objects there to those in your code, they won't work.
Third:
If it still does not work, try adding this line to your viewDidLoad
[self.modeChangeButton addTarget:self #selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
Fourth:
Change this line in your viewDidLoad:
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
to:
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
This should solve your problem.
This code is adding a new button on top of your self.modeChangeButton which I'm assuming is coming from a xib or storyboard.
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
Do this instead...
- (void)viewDidLoad {
self.recordMode = YES;
}
- (void)setRecordMode:(BOOL)recordMode {
_recordMode = recordMode;
UIImage *imageForMode = recordMode ? [UIImage imageNamed:#"photomode.png"] : [UIImage imageNamed:#"recordmode.png"];
[self.modeChangeButton setBackgroundImage:imageForMode forState:UIControlStateNormal];
}
- (IBAction)modeButtonClick:(id)sender {
self.recordMode = !self.recordMode;
}
Check your button size and make the frame size closer to the image size. And try to maintain the two images as like below.
- (void)viewDidLoad
{
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[modeChangeButton setImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender
{
sender.selected = !sender.selected;
}
Try modifying you viewDidLoad method as follows.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
// You might need to add following two lines into your code.
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
Above solution should work. As I see, you have referencing property named modeChangeButton in your modeButtonClick: method. But you are not assigning the created button into your property. Also you have to set the action selector to your button.
As a improvement, you can do this to improve performance of your application. Without setting the background image on click event, you can select/deselect the button. With this you don't need to maintain boolean flags, which may introduce new bugs to your application.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
[self.modeChangeButton setSelected:![self.modeChangeButton isSelected]];
}
Hope this helps.
i am creating one iPad application in that i want to open media player to play video. For this i create one button in ViewDidLoad() method. Bellow is my code.
- (void)viewDidLoad
{
[super viewDidLoad]
self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"1.png"]] autorelease];
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
m_btn2000.frame = CGRectMake(180, 330, 130, 200);
[m_btn2000 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
m_btn2000.clipsToBounds = YES;
[m_btn2000 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_btn2000];
}
Bellow is my button clicked method code:
- (void)btnClicked:(id)sender
{
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:#"test_video" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath1];
m_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
m_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[m_moviePlayer.view setFrame:CGRectMake(0, 0, 1026, 748)];
[self.view addSubview:m_moviePlayer.view];
[m_moviePlayer play];
}
when i m going to click on button it gives me EXC_BAD_ACCESS error.
But i notice one thing that when i put above whole code which in button clicked method putting in ViewDidLoad() method it play a video in player proper way.
The reason is : you are creating a button with + (factory method) and you are sending autorelease message.
you need to change this line from:
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
to
m_btn2000 = [UIButton buttonWithType:UIButtonTypeCustom];