How to detect YTPlayerState.Ended using Swift? - ios

I'm playing You Tube videos in my app and trying to detect when a video is paused or ended? I've found the kYTPlayerState function in the .m file, but having trouble converting it's use to Swift. Here is what I have....
func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState)
{
switch (state) {
case YTPlayerState.Playing:
print("Started playback");
break;
case YTPlayerState.Paused:
print("Paused playback");
break;
case YTPlayerState.Ended:
print("Ended playback");
break;
default:
break;
}
}
and .....
if playerView(player , didChangeToState: YTPlayerState.Ended)
{
/// Do Something....
}
....which I place in ViewDidLoad(). I'm also getting back the error on my if statement that says Type "()" does not conform to protocol "Boolean Type" . If you know how to fix this, or if I'm leaving something out please help.
I'm initializing the player in this view with #IBOutlet var player: YTPlayerView!

playerView:didChangeToState is a delegate method, a callback. You shouldn't be calling it directly like in your second code block. If you set the YTPlayerViewDelegate to your view controller (player.delegate = self in viewDidLoad) playerView:didChangeToState should be called automatically for you as player changes state. Just make sure you implement the state change method.
func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState)
{
case YTPlayerState.Ended:
// handle ended state
break;
default:
break;
}
}

Related

Check for condition befor executing button selector

I have multiple buttons with different functions, now these buttons should check for a condition befor executing the actual functionality, I don't won't to change all buttons functions like the following:
#objc func btnAction(sender: UIButton) {
if condition {
runSomthing()
return
} else {
run the actual btn action
}
}
I need to extract this to a superclass that accomplishes what I looking for without changing the current button actions
Is there a correct way to do it?
One of the solutions is to link all the buttons to the same IBAction and perform corresponding button functionalities using the button tag value. This prevents the condition checking in each IBAction.
guard let button = sender as? UIButton else {
return
}
if condition {
runSomthing()
return
} else {
switch button.tag {
case 1:
Perform operation A
case 2:
Perform operation B
case 3:
Perform operation C
default:
print("None")
return
}
}
Enums can also be used instead of tag value for better identification. I referred How to use one IBAction for multiple buttons in Swift?

How to use self as delegate to different UIImagePickerController

Context: I have two functions inside my controller that both present a UIImagePickerController with delegate=self
I should do different actions with the image received based on which method spawned de picker
In android I can set a requestCode that behaves like a unique request identifier so when the result comes in delegate function (in this case func imagePickerController) i can differentiate it and i know what to do with it
I don't seem to find anything to differentiate inside delegate function "imagePickerController" so I know which picker caused to method to be called
You can simply add a variable in your self.
var currentPickerId : Int?
Then when you set the delegate:
picker.delegate = self
currentPickerId = 1
And in your delegate method:
switch currentPickerId {
case 1: // Do whatever you want
case 2: // And so on
default: break
}
// And don't forget to reinit the id, you never know... ;p
currentPickerId = nil

Identify which UISwitch is tapped

I have multiple UISwitches on settings view, and linked all UISwitches to same IBAction function.
#IBAction func settingSwitchTapped(sender: AnyObject) {
}
How do i find which UISwitch is tapped so that i just save only the switch that actually changed (instead of saving all others also)
That's the sole purpose of the sender parameter.
If all your switches are defined as IBOutlets, you can compare sender to each of them in order to know which one you've tapped.
You can also give each of them a different tag property, and decide what to do based on sender.tag.
Set tag for each Switch with a unique no.
you can get exact switch by using following :
switch = sender.tag
Hope it helps
#IBAction func settingSwitchTapped(sender: AnyObject) {
switch (sender.tag){
case 101: // your tag
// executable code goes here
break;
case 102: // your tag
break;
case 103: // your tag
break;
default:
break;
}
}

iOS: Switch UIViews using ReactiveCocoa

I am having some trouble kicking-off with ReactiveCocoa
So basically - I have 2 (potentially N) UIButtons which when tapped on should trigger a different view to be displayed on the screen.
At the moment I am achieving it in the following way:
- (IBAction)firstButtonTapped:(id)sender {
[self processViewDisplayForViewType:ViewTypeFirstType];
}
- (IBAction)secondButtonTapped:(id)sender {
[self processViewDisplayForViewType:ViewTypeSecondType];
}
-(void)processViewDisplayForViewType:(ViewType)viewType{
switch (viewType) {
case ViewTypeFirstType:
//process showing a view based on the given type
break;
case ViewTypeSecondType:
//process showing a view based on the given type
break;
default:
break;
}
self.currentViewType = viewType;
}
Now, if I have other display types I would need to add aditional statements in the switch block... so I hope I can solve this using ReactiveCocoa and FRP. Anybody can point me in the right direction on solving this ?

segmented control: titleForSegmentAtIndex returns null every time

I have a segmented control (Added via story board) and have set two segments (set in story board). When it is clicked it fires my 'segmentedIndexChanged:' method, but in that method i can't get the values (it always returns null) for either .selectedSegmentedIndex or titleForSegmentedAtIndex
-(IBAction) segmentedIndexChanged {
NSLog(#"index, text value: %# , %#", self.mySegControl.selectedSegmentIndex, [mySegControl titleForSegmentAtIndex:mySegControl.selectedSegmentIndex]
);
switch (self.mySegControl.selectedSegmentIndex) {
case 0:
NSLog(#"segmented 1 selected");
break;
case 1:
NSLog(#"segmented 2 selected");
break;
default:
break;
}
}
any ideas why?
Are you sure the outlet is connected to the control? You can check by printing out NSLog(#"%#", self.mySegControl)
Also, usually, IBActions should have the form of - (IBAction)action:(id)sender, that way you would be able to access the sender (in this case the segmented control) without having an outlet to it as well.

Resources