I have a screen with Two UISwitch buttons.
I am receiving a data in API:
"CustomerSubscribed": [
{
"Customer_ID": "100132",
"Mobile": "3213103428",
"Subscription": "Bill Payment",
"Mnemonic": "BILL",
"IsActive": 0
},
{
"Customer_ID": "100132",
"Mobile": "3213103428",
"Subscription": "Funds Transfer",
"Mnemonic": "IBFT",
"IsActive": 1
}
]
These are my two switches:
private var fundSwitch: UISwitch = {
let switchView = UISwitch()
switchView.isOn = false
switchView.onImage = UIImage(named: "switchOn")
switchView.offImage = UIImage(named: "switchOff")
switchView.onTintColor = UIColor.purple
switchView.tag = 1
switchView.addTarget(self, action: #selector(switchStateDidChange), for: .valueChanged)
switchView.translatesAutoresizingMaskIntoConstraints = false
return switchView
}()
private var billSwitch: UISwitch = {
let switchView = UISwitch()
switchView.isOn = false
switchView.onImage = UIImage(named: "switchOn")
switchView.offImage = UIImage(named: "switchOff")
switchView.onTintColor = UIColor.purple
switchView.tag = 2
switchView.addTarget(self, action: #selector(switchStateDidChange), for: .valueChanged)
switchView.translatesAutoresizingMaskIntoConstraints = false
return switchView
}()
On the basis of isActive, I have to set the values of UISWitch. If it is true, then switch must be on, otherwise if it is false then it must be Off.
This is what im trying right now:
@objc func switchStateDidChange(_ sender: UISwitch) {
if sender.tag == Switch.fundTrasnfer.rawValue {
print( sender.isOn ? customerSubscribe?[0].isActive ?? false: "Fund Transfer Switch is Off" )
} else {
print( sender.isOn ? customerSubscribe?[1].isActive ?? false: "Bill Payment Fund Transfer Switch is Off" )
}
}
and another what I tried:
@objc func switchStateDidChange(_ sender: UISwitch) {
for customer in self.customerSubscribe! {
if customer.isActive == true {
print(sender.isOn ? "Switch is ON": "Switch is Off")
} else {
print(sender.isOn ? "Switch is ON": "Switch is Off")
}
}
}
these are two approaches I have tried. I am using VIP Architecture, and I received response here:
extension SubscriptionViewController: SubscribeViewProtocol {
func saveSuccessCustomerData(response: CustomerSubscribeEntity.CustomerServiceResponse) {
}
func successSetupSubscription(response: SubscriptionEntity.SubscriptionServicesResponse) {
DispatchQueue.main.async {
print("Id \(response.customerSubscribed?[0].customerID)")
self.customerSubscribe = response.customerSubscribed
self.fundTransferLbl.text = self.customerSubscribe?[0].subscription
self.billPaymentLbl.text = self.customerSubscribe?[1].subscription
}
}
}
Then I created customerSubscribe to have the response in it:
var customerSubscribe: [SubscriptionEntity.CustomerSubscribed]?
My screen:
UI Is is programmatically, and i have both fund and bill switch defined.
Can I make a loop and check too instead of comparing taking index?
CodePudding user response:
You are still hiding a lot of implementation detail that would be neccassary to give a definitive answer. So I have to make some assumptions.
isActive
is of type booleansuccessSetupSubscription
lives in the same place as your switch vars- you create your switches programaticly and add them to your view.
Your function switchStateDidChange
will react to changes done by the user. If you want to change the state of the switch programmaticly you need to set it to the switch itself.
func successSetupSubscription(response: SubscriptionEntity.SubscriptionServicesResponse) {
DispatchQueue.main.async {
print("Id \(response.customerSubscribed?[0].customerID)")
self.customerSubscribe = response.customerSubscribed
self.fundTransferLbl.text = self.customerSubscribe?[0].subscription
self.billPaymentLbl.text = self.customerSubscribe?[1].subscription
//add this
self.fundSwitch.isOn = response.customerSubscribed[0].isActive
self.billSwitch.isOn = response.customerSubscribed[1].isActive
}
}
CodePudding user response:
You can try
switchOne.isOn = customerSubscribe?.first?.isActive == 1
switchTwo.isOn = customerSubscribe?.last?.isActive == 1