In my project I am using 6 buttons in one screen.I want to change the button color based on user's tap and "day order value". i am getting "day order value" from server.for example user day order value is equal to 1, day1 button background color should be red if user taps on day2 button day2 button should be in blue and remaining all button background colour should be white.
Please see the below screenshot.
if user pressed on one button that particular button should be highlight with some color remaining buttons should be same color. I can able to change button color by checking each condition but I want to write in simple manner.
see the following code which i have i tried for "dayoredrvalue".
func UpdateButtoncolor()
{
let dayOrderStr = UserDefaults.standard.string(forKey: "dayOrderStr")
print("dayOrderStr--",dayOrderStr)
if (dayOrderStr?.isEqual("1"))!{
self.day1Btn.backgroundColor = UIColor.red
self.day2Btn.backgroundColor = UIColor.white
self.day3Btn.backgroundColor = UIColor.white
self.day4Btn.backgroundColor = UIColor.white
self.day5Btn.backgroundColor = UIColor.white
self.day6Btn.backgroundColor = UIColor.white
}else if(dayOrderStr?.isEqual("2"))!
{
self.day1Btn.backgroundColor = UIColor.white
self.day2Btn.backgroundColor = UIColor.red
self.day3Btn.backgroundColor = UIColor.white
self.day4Btn.backgroundColor = UIColor.white
self.day5Btn.backgroundColor = UIColor.white
self.day6Btn.backgroundColor = UIColor.white
}else if(dayOrderStr?.isEqual("3"))!
{
self.day1Btn.backgroundColor = UIColor.white
self.day2Btn.backgroundColor = UIColor.white
self.day3Btn.backgroundColor = UIColor.red
self.day4Btn.backgroundColor = UIColor.white
self.day5Btn.backgroundColor = UIColor.white
self.day6Btn.backgroundColor = UIColor.white
}else if(dayOrderStr?.isEqual("4"))!
{
self.day1Btn.backgroundColor = UIColor.white
self.day2Btn.backgroundColor = UIColor.white
self.day3Btn.backgroundColor = UIColor.white
self.day4Btn.backgroundColor = UIColor.red
self.day5Btn.backgroundColor = UIColor.white
self.day6Btn.backgroundColor = UIColor.white
}else if(dayOrderStr?.isEqual("5"))!
{
self.day1Btn.backgroundColor = UIColor.white
self.day2Btn.backgroundColor = UIColor.white
self.day3Btn.backgroundColor = UIColor.white
self.day4Btn.backgroundColor = UIColor.white
self.day5Btn.backgroundColor = UIColor.red
self.day6Btn.backgroundColor = UIColor.white
}else
{
self.day1Btn.backgroundColor = UIColor.white
self.day2Btn.backgroundColor = UIColor.white
self.day3Btn.backgroundColor = UIColor.white
self.day4Btn.backgroundColor = UIColor.white
self.day5Btn.backgroundColor = UIColor.white
self.day6Btn.backgroundColor = UIColor.red
}
}
As per your query, It's quite simple. Just follow the following -
Step 1: Add an UIButton object in your viewController. Like that -
var selectedButton: UIButton? = nil
Step 2: Add same button Action for all your buttons -
#IBAction func btnActionSelection(_ sender:UIButton){
if(selectedButton == nil){ // No previous button was selected
updateButtonSelection(sender)
}else{ // User have already selected a button
if(selectedButton != sender){ // Not the same button
selectedButton?.backgroundColor = .clear
updateButtonSelection(sender)
}
}
}
Step 3: Now, Update button selection
func updateButtonSelection(_ sender: UIButton){
UserDefaults.standard.set("\(sender.tag)", forKey: "dayOrderStr")
selectedButton = sender
selectedButton?.backgroundColor = .green
}
Step 4: Check User selected day (For that you need to add tag on buttons from 1 to 6 respectively)
override func viewDidLoad() {
super.viewDidLoad()
// Check user's selected day
if let selectedDay = UserDefaults.standard.value(forKey: "dayOrderStr") as? String{
debugPrint("selectedDay: "selectedDay) // Get user's selected day
if let btn = self.view.viewWithTag(Int(selectedDay)!){
updateButtonSelection(btn)
}
}
//Other stuff
}
I have attached a demo for you using my logic:
For this you need to take a Group Outlets of your UIButton and assign a unique tag to each and then rest logic is described in the demo attached below.
like
#IBOutlet var btnAllSelected: [UIButton]!
And then simple logic like this:
#IBAction func btnAllClicked(_ sender: UIButton) {
for button in btnAllSelected {
if sender.tag == button.tag{
button.isSelected = true;
button.backgroundColor = UIColor.green
}else{
button.isSelected = false;
button.backgroundColor = UIColor.red
}
}
if sender.tag == 1{
print("Button 1 selected")
}else if sender.tag == 2{
print("Button 2 selected")
}else if sender.tag == 3{
print("Button 3 selected")
}else if sender.tag == 4{
print("Button 4 selected")
}else if sender.tag == 5{
print("Button 5 selected")
}else if sender.tag == 6{
print("Button 6 selected")
}
}
Link to the Demo
FYI:- Please ignore the pods installed in it. I edited some another demo and made a one for you.
Hope this helps.
You should create New Referencing Outlet Collections for all your UIButtons like this,
#IBOutlet var arrButtons: [UIButton]!
Implement only one Button tap events for all your buttons, Please find belo code.
#IBAction func btnClicked(_ sender: UIButton) {
arrButtons.forEach({$0.backgroundColor = UIColor.red})
sender.backgroundColor = UIColor.darkGray
}
Simplified code, it uses an array of the buttons and an index:
let buttonArray = [day1Btn, day2Btn, day3Btn, day4Btn, day5Btn, day6Btn]
buttonArray.forEach { $0.backgroundColor = .white }
guard let dayOrderStr = UserDefaults.standard.string(forKey: "dayOrderStr"),
let dayOrderIndex = Int(dayOrderStr), 1...buttonArray.count ~= dayOrderIndex {
buttonArray[dayOrderIndex-1].backgroundColor = .red
}
It can be still simpler if you save the value in UserDefaults as Int
Here is one solution among many others.
#IBOutlet var buttons: [UIButton]! // link all the buttons from the storyboard
func changeColorButton(sender: UIButton) {
// default appearance for all buttons
for button in buttons {
button.backgroundColor = UIColor.green
}
// update color only for the button clicked
sender.backgroundColor = .orange
}
#IBAction func buttonTapped(_ sender: UIButton) {
updateFocusPicture(sender: sender)
}
I am trying learning the UISegmentedControl and want to figure out how to get it to work within an if statement to return one value based on selection and another value based on the other selection. There is only two values the UISegment can return - "Fixed" or "Varibale". The code below does not work but gives an idea what I want. The two variables are predefined.
#IBAction func calPenalty(_ sender: UIButton) {
let startDate = termStartDate.text
let mortgageTerm = Double(mortgageTermLabel.text!)
let discount = Double(mortgageDiscountLabel.text!)
let mtgCashback = Double(casbackRecieved.text!)
let mtgBalance = Double(mortgageBalance.text!)
let rate = Double(currentRate.text!)
//let mortgageType = mortgageType
let lender = Global.selectedRate
if let oneYear = Double((lender?.oneYear)!),
let twoYear = Double((lender?.twoYear)!),
let threeYear = Double((lender?.threeYear)!),
let fourYear = Double((lender?.fourYear)!),
let fiveYear = Double((lender?.fiveYear)!) {
let maturityDate = (getMaturityDate(startdate: startDate!, termMonths: Int(mortgageTerm!)))
let monthsToMaturity = daysBetweenDates(endDate: maturityDate)/365*12
let comparisonTerm = (IRDComparisonTerm(monthsToMaturity: monthsToMaturity))
let cashback = cashbackRepayment(cashbackRecieved: mtgCashback!, mtgTerm: Double(mortgageTerm!), mthsToMaturity: Double(monthsToMaturity))
print(cashback)
var comparisonRate: Double = 0
switch comparisonTerm
{
case 12:
comparisonRate = oneYear
case 24:
comparisonRate = twoYear
case 36:
comparisonRate = threeYear
case 48:
comparisonRate = fourYear
case 60:
comparisonRate = fiveYear
default:
comparisonRate = 0
} // end switch statement
print(comparisonRate)
let IRD = IRDPenalty(IRDComparisonRate: comparisonRate, mtgBalance: mtgBalance!, mthsToMaturity: Double(monthsToMaturity), discount: discount!, currentRate: rate!)
let threeMthsInterestPenalty = threeMonthsInterestPenalty(mtgBalance: mtgBalance!, mtgRate: rate!)
print (IRD)
print (threeMthsInterestPenalty)
var penalty: Double = 0
// var totalPenalty: Double = 0
if IRD > threeMthsInterestPenalty {
penalty = IRD + cashback
}else{
penalty = threeMthsInterestPenalty + cashback
}
// totalPenalty = penalty + cashback
calculationLabel.text = String(penalty)
}
}
// triggers result based on value selected
#IBAction func valueChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
calculationLabel.text = String(penalty)
case 1:
calculationLabel.text = String(threeMthsInterestPenalty + cashback)
default:
calculationLabel.text = String(penalty)
}
}
Edit: I am still working on this problem. I have updated to the above code to show all the code within the IBAction. The #IBAction does not work as the variables are undefined based on where they are posted within the code. The Storyboard has a UIsegmentedControl with to values "Fixed and Variable. If the user selects fixed then I want it to show as per the IBAction for UISegement Control.
If what you want is to check when the value of your segmented control changes, and you want to do it programmatically, you firstly need to register a target-action method using valueChanged constant. Then, write up your valueChanged function. Check this out for more information.
import UIKit
class ViewController: UIViewController {
// Reference from Storyboard or Interface Builder
#IBOutlet weak var mortgageType: UISegmentedControl!
// For set up a UISegmentedControl programmatically
var segmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
override func loadView() {
super.loadView()
// Programmatically setup
let items = ["Cinnamon", "Clove"]
segmentedControl = UISegmentedControl(items: items)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.frame = CGRect(x: 120, y: 200, width: 200, height: 30)
segmentedControl.tintColor = UIColor.black
segmentedControl.addTarget(self, action: #selector(self.valueChanged(_:)), for: .valueChanged)
self.view.addSubview(segmentedControl)
}
#IBAction func valueChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
print("Hi")
case 1:
print("Bye")
default:
print("Nothing")
}
}
}
If you reference your UISegmentedControl from Storyboard and Interface Builder. You should also add an action to it.
Update to the edited question
#IBAction func calPenalty(_ sender: UIButton) {
if mortgageType.selectedSegmentIndex == 0 {
print("Fixed")
} else {
print("Variable")
}
}
I currently have a UITextfield with an eye icon in it that when pressed is supposed to toggle the secure text entry on and off.
I know you can check mark the "secure text entry" box in the attributes inspector but how to do it so it toggles whenever the icon is pressed?
Use this code,
iconClick is bool variable, or you need other condition check it,
var iconClick = true
eye Action method:
#IBAction func iconAction(sender: AnyObject) {
if iconClick {
passwordTF.secureTextEntry = false
} else {
passwordTF.secureTextEntry = true
}
iconClick = !iconClick
}
hope its helpful
An unintended side-effect of this is that if the user toggles to insecure, and then back to secure, the existing text will be cleared if the user continues typing. The cursor may also end up in the wrong position unless we reset the selected text range.
Below is an implementation that handles these cases (Swift 4)
extension UITextField {
func togglePasswordVisibility() {
isSecureTextEntry = !isSecureTextEntry
if let existingText = text, isSecureTextEntry {
/* When toggling to secure text, all text will be purged if the user
continues typing unless we intervene. This is prevented by first
deleting the existing text and then recovering the original text. */
deleteBackward()
if let textRange = textRange(from: beginningOfDocument, to: endOfDocument) {
replace(textRange, withText: existingText)
}
}
/* Reset the selected text range since the cursor can end up in the wrong
position after a toggle because the text might vary in width */
if let existingSelectedTextRange = selectedTextRange {
selectedTextRange = nil
selectedTextRange = existingSelectedTextRange
}
}
}
This snippet is using the replace(_:withText:) function because it triggers the .editingChanged event, which happens to be useful in my application. Just setting text = existingText should be fine as well.
Why to use an extra var. In the action method of the eye button just do as below
password.secureTextEntry = !password.secureTextEntry
UPDATE
Swift 4.2 (as per #ROC comment)
password.isSecureTextEntry.toggle()
I wrote extension for the same. To provide Password toggle.
In your Assets first add images that you want for toggle.
Add following extension for UITextField.
extension UITextField {
fileprivate func setPasswordToggleImage(_ button: UIButton) {
if(isSecureTextEntry){
button.setImage(UIImage(named: "ic_password_visible"), for: .normal)
}else{
button.setImage(UIImage(named: "ic_password_invisible"), for: .normal)
}
}
func enablePasswordToggle(){
let button = UIButton(type: .custom)
setPasswordToggleImage(button)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
self.rightView = button
self.rightViewMode = .always
}
#IBAction func togglePasswordView(_ sender: Any) {
self.isSecureTextEntry = !self.isSecureTextEntry
setPasswordToggleImage(sender as! UIButton)
}
}
Call extension on your UITextField Outlet
override func viewDidLoad() {
super.viewDidLoad()
txtPassword.enablePasswordToggle()
txtConfirmPassword.enablePasswordToggle()
}
Swift 4 solution
You don't need extra if statement for simple toggle isSecureTextEntry property
func togglePasswordVisibility() {
password.isSecureTextEntry = !password.isSecureTextEntry
}
But there is a problem when you toggle isSecureTextEntry UITextField doesn't recalculate text width and we have extra space to the right of the text. To avoid this you should replace text this way
func togglePasswordVisibility() {
password.isSecureTextEntry = !password.isSecureTextEntry
if let textRange = password.textRange(from: password.beginningOfDocument, to: password.endOfDocument) {
password.replace(textRange, withText: password.text!)
}
}
UPDATE
Swift 4.2
Instead of
password.isSecureTextEntry = !password.isSecureTextEntry
you can do this
password.isSecureTextEntry.toggle()
Use UITextFiled rightView to show toggle button
var rightButton = UIButton(type: .custom)
rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
yourtextfield.rightViewMode = .always
yourtextfield.rightView = rightButton
If you need TextField with similar feature in multiple places its best to subclass the UITextField like follwing example -
import UIKit
class UIShowHideTextField: UITextField {
let rightButton = UIButton(type: .custom)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
required override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
rightButton.setImage(UIImage(named: "password_show") , for: .normal)
rightButton.addTarget(self, action: #selector(toggleShowHide), for: .touchUpInside)
rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
rightViewMode = .always
rightView = rightButton
isSecureTextEntry = true
}
#objc
func toggleShowHide(button: UIButton) {
toggle()
}
func toggle() {
isSecureTextEntry = !isSecureTextEntry
if isSecureTextEntry {
rightButton.setImage(UIImage(named: "password_show") , for: .normal)
} else {
rightButton.setImage(UIImage(named: "password_hide") , for: .normal)
}
}
}
After which you can use it in any ViewController,
class ViewController: UIViewController {
#IBOutlet var textField: UIShowHideTextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.becomeFirstResponder()
}
}
For Objective c
set image for RightButton In viewdidload Method
[RightButton setImage:[UIImage imageNamed:#"iconEyesOpen"] forState:UIControlStateNormal];
[RightButton setImage:[UIImage imageNamed:#"iconEyesClose"] forState:UIControlStateSelected];
and then set action method for that RightButton
-(IBAction)RightButton:(id)sender
{
if (_rightButton.selected)
{
_rightButton.selected = NO;
_passwordText.secureTextEntry = YES;
if (_passwordText.isFirstResponder) {
[_passwordText resignFirstResponder];
[_passwordText becomeFirstResponder];
}
}
else
{
_rightButton.selected = YES;
_passwordText.secureTextEntry = NO;
if (_passwordText.isFirstResponder) {
[_passwordText resignFirstResponder];
[_passwordText becomeFirstResponder];
}
}
}
Swift 3
// MARK: Btn EyeAction
#IBAction func btnEyeAction(_ sender: Any) {
if(iconClick == true) {
txtPassword.isSecureTextEntry = false
iconClick = false
} else {
txtPassword.isSecureTextEntry = true
iconClick = true
}
}
Shortest!
I think this is the shortest solution for secure entry as well as updating the picture of the button.
#IBAction func toggleSecureEntry(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
textfieldPassword.isSecureTextEntry = !sender.isSelected
}
Assign the show/hide picture of the button according to the state selected /default , no need to create any variable or outlet.
This worked for me on Swift 5.0
#IBAction func changePasswordVisibility(_ sender: UIButton) {
passwordField.isSecureTextEntry.toggle()
if passwordField.isSecureTextEntry {
if let image = UIImage(systemName: "eye.fill") {
sender.setImage(image, for: .normal)
}
} else {
if let image = UIImage(systemName: "eye.slash.fill") {
sender.setImage(image, for: .normal)
}
}
}
Button attributes:
Result:
Swift 3
passwordTF.isSecureTextEntry = true
passwordTF.isSecureTextEntry = false
#IBAction func eye_toggle_clicked(sender: AnyObject)
{
if toggleBtn.tag == 0
{
passwordTxt.secureTextEntry=true
toggleBtn.tag=1
}
else
{
passwordTxt.secureTextEntry=false
toggleBtn.tag=0
}
}
As others have noted, the property is secureTextEntry, but you won't find this in the UITextField documentation, as it is actually inherited by a UITextField through the UITextInputTraits protocol- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/#//apple_ref/occ/intfp/UITextInputTraits/secureTextEntry
You can simply toggle this value each time your button is tapped:
#IBAction func togglePasswordSecurity(sender: UIButton) {
self.passwordField.secureTextEntry = !self.passwordField.secureTextEntry
}
try this line:
#IBAction func btnClick(sender: AnyObject) {
let btn : UIButton = sender as! UIButton
if btn.tag == 0{
btn.tag = 1
textFieldSecure.secureTextEntry = NO
}
else{
btn.tag = 0
textFieldSecure.secureTextEntry = NO;
}
}
Here is your answer no need to take any bool var:
#IBAction func showHideAction(sender: AnyObject) {
if tfPassword.secureTextEntry{
tfPassword.secureTextEntry = false
}else{
tfPassword.secureTextEntry = true;
}
}
First you need to set image(visible or hide) of button of eye for different state (selected or normal)
than connect IBAction and write code like
#IBAction func btnPasswordVisiblityClicked(_ sender: Any) {
(sender as! UIButton).isSelected = !(sender as! UIButton).isSelected
if (sender as! UIButton).isSelected {
txtfPassword.isSecureTextEntry = false
} else {
txtfPassword.isSecureTextEntry = true
}
}
In Swift 4
var iconClick : Bool!
override func viewDidLoad() {
super.viewDidLoad()
iconClick = true
}
#IBAction func showHideAction(_ sender: Any)
{
let userPassword = userPasswordTextFiled.text!;
if(iconClick == true) {
userPasswordTextFiled.isSecureTextEntry = false
iconClick = false
} else {
userPasswordTextFiled.isSecureTextEntry = true
iconClick = true
}
}
Assignment values change from YES/NO to true/false boolean values.
password.secureTextEntry = true //Visible
password.secureTextEntry = false //InVisible
You can try this code..
i think it's helpful.
Use button with eye image
and make buttonHandler method
set Tag for button with value 1
-(IBAction) buttonHandlerSecureText:(UIButton *)sender{
if(sender.tag ==1){
[self.textField setSecureTextEntry:NO];
sender.tag = 2;
}
else{
[self.textField setSecureTextEntry:YES];
sender.tag = 1;
}
}
For Xamarin folks:
passwordField.SecureTextEntry = passwordField.SecureTextEntry ? passwordField.SecureTextEntry = false : passwordField.SecureTextEntry = true;
Try this code in swift 4, tried to make a reusable code within a controller. I have set different image for buttons in storyboard as shown in the link https://stackoverflow.com/a/47669422/8334818
#IBAction func clickedShowPassword(_ sender: UIButton) {
var textField :UITextField? = nil
print("btn ",sender.isSelected.description)
switch sender {
case encryptOldPswdBtn:
encryptOldPswdBtn.isSelected = !encryptOldPswdBtn.isSelected
textField = oldPasswordTextField
default:
break
}
print("text ",textField?.isSecureTextEntry.description)
textField?.isSecureTextEntry = !(textField?.isSecureTextEntry ?? false)
}
#objc func togglePasscode(){
switch textfield.isSecureTextEntry{
case true:
textfield.isSecureTextEntry = false
case false:
textfield.isSecureTextEntry = true
}
}
Here is a easy and more readable solution using Switch statement.
Hope this is simpler solution rather than creating a BOOL object globally.
#IBAction func passwordToggleButton(sender: UIButton) {
let isSecureTextEntry = passwordTextField.isSecureTextEntry
passwordTextField.isSecureTextEntry = isSecureTextEntry ? false : true
if isSecureTextEntry {
visibilityButton.setImage(UIImage(named: "visibility"), for: .normal)
} else {
visibilityButton.setImage(UIImage(named: "visibility_off"), for: .normal)
}
}
only add this line into your code replace you TextField name with "textfield" Done:
you need to change the isSecureTextEntry propertity to change true for password type textFiled like ......
textField.isSecureTextEntry = true
sender.isSelected = !sender.isSelected
if(sender.isSelected == true) {
RegPasswordField.isSecureTextEntry = false
sender.setBackgroundImage(UIImage(systemName: "eye.fill"), for: .normal)
} else {
RegPasswordField.isSecureTextEntry = true
sender.setBackgroundImage(UIImage(systemName: "eye"), for: .normal)
}
Swift 5 Please use this
var btnClick = true
if(btnClick == true) {
passwordTextField.isSecureTextEntry = false
} else {
passwordTextField.isSecureTextEntry = true
}
btnClick = !btnClick
}
var viewingPassword = true
#IBAction func btnEyeAction(_ sender: Any) {
passwordTF.isSecureTextEntry = viewingPassword ? false : true
viewingPassword.toggle()
}
I make simple player with VK Api. I want to do this.
When I clicked first track and then second change first button title "stop" to "play" automatically.
How to do it?
My play/stop button action.
func playAction(sender: UIButton!) {
if sender.currentTitle == "P" {
let track = dataOfTracks[sender.tag] as trackDoc
let url = NSURL(string: track.data.url)
player = AVPlayer(URL: url!)
player.play()
sender.setTitle("S", forState: UIControlState.Normal)
} else if sender.currentTitle == "S" {
sender.setTitle("P", forState: UIControlState.Normal)
player.pause()
}
}
EDIT CODE DETAILS:
don't reallocate every time the button. It must be another outlet of the SongsTableViewCell class (same as the label). And set the target/action from the Interface Builder (add #IBAction in front of "func cellSongClicked" and ctrl-drag from IB)
2.add the following property to your class:private var currentSong : Int?
3) method cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SongTitleCell", forIndexPath: indexPath) as SongsTableViewCell
let songDic : NSDictionary = arrSongs.objectAtIndex(indexPath.row) as NSDictionary
cell.lblSongTitle.text = songDic.objectForKey("SongTitle") as? String
cell.btnPlayPause.tag = indexPath.row
var title : String
if let _currentSong = currentSong {
title = indexPath.row == _currentSong ? "Stop" : "Play"
} else {
title = "Play"
}
cell.btnPlayPause.setTitle(title, forState: UIControlState.Normal)
return cell
}
4) And the action:
#IBAction func cellSongClicked (sender : AnyObject ){
var remote = GetSongData()
remote.delegate = self
var btnCurrentPressed = sender as UIButton
//Play Selected Song
let songDic : NSDictionary = arrSongs.objectAtIndex(btnCurrentPressed.tag) as NSDictionary
var rowsToReload = [NSIndexPath]()
var stopCurrent = false
if let _currentSong = currentSong {
if btnCurrentPressed.tag == _currentSong {
stopCurrent = true
} else {
rowsToReload.append(NSIndexPath(forRow: _currentSong, inSection:0))
}
}
rowsToReload.append(NSIndexPath(forRow: btnCurrentPressed.tag, inSection:0))
currentSong = stopCurrent ? nil : btnCurrentPressed.tag
self.tableView.reloadRowsAtIndexPaths(rowsToReload, withRowAnimation: .None)
}
You don't have to change the title of the button every time. You could use something like this:
func viewDidLoad {
playButton.setTitle("Play", forState: UIControlState.Normal)
playButton.setTitle("Stop", forState: UIControlState.Selected)
}
Then in your callback method you just change the state of the button and the text will get updated too:
func playAction(sender: UIButton!) {
// Selected means the content is playing
if !sender.selected {
let track = dataOfTracks[sender.tag] as trackDoc
let url = NSURL(string: track.data.url)
player = AVPlayer(URL: url!)
player.play()
} else {
player.pause()
}
// update the state of the button
sender.selected = !sender.selected
}
I consider this to be a more clean solution since you are not comparing strings anymore to make your decision and you are actually using the binary state of the button to keep track of the current state of your content(which is also binary, either playing or not).
If you have more questions, I would be more than happy to help you further, just let me know.