Identify which UISwitch is tapped - ios

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;
}
}

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?

Swift - How can I identify which custom tableview cell has been modified?

I have three custom cell types - a cell with a text field, another with a date field, and another with a picker field.
I'm capturing data changes here:
func textFieldDidEndEditing(_ textField: UITextField) {
print("TextField did end editing method called")
switch segmentedControl.selectedSegmentIndex {
case 0:
currentItem.setObject(itemName, forKey: "itemName")
.....
currentItem.setObject(serialNumber, forKey: "serialNumber")
break
case 1:
currentItem.setObject(username, forKey: "username")
currentItem.setObject(email, forKey: "email")
...
break
The "currentItem" is a CKRecord.
This is wrong. The fields (itemName, serialNumber, etc) are strings. They are set in ViewDidLoad with cloud data.
How do I correctly update these fields
Thanks in advance
As Subin K Kuriakose has said in the comments, you should use the tag property of UITextField to figure out which text field it is.
You need to set the tag property when you create the text field cell. Just keep a variable somewhere and increment it each time a text field is created, and set the tag to this variable:
textFieldCount += 1
myCustomCell.textField.tag = textFieldCount
Something like that, you get the idea.
Now each text field in your table view has a unique tag. In textFieldDidEndEditing, you check the tag:
switch textField.tag {
case 1:
// it's the first text field!
case 2:
// it's the second text field!
case 3:
// it's the third text field!
default:
break
}
It's simple!

How to detect YTPlayerState.Ended using Swift?

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;
}
}

Can't switch state of UISwitch

I have to UISwitches in my application. At launch, they're both set to off and the second switch is disabled and should only be enabled when the first switch is aswell, meaning:
#IBAction func switchOneToggled(sender: UISwitch) {
if switchOne.on {
switchTwo.enabled = true
}
else {
switchTWo.enabled = false
}
}
My problem is, that when I enable switchOne, switchTwo gets enabled, but I can't toggle switchTwo's on/off state by touching it.
Try resetting the switches. I had a similar problem and just deleted the switch and replaced it, and giving it the same connections that is had before. It must be a bug inside Xcode, since it should be able to turn on and off without any code.
#IBAction func switchOneToggled(sender: UISwitch) {
switchTwo.enabled = switchOne.on
}
do you maybe have userinteractionenabled set to false for switchTwo?
The problem is that your switchOneToggled is called when the Switch is tapped and the state of the switch is changed. You should check the state of Switch 1 and enable your Switch 2 in your viewDidLoad.

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