Trouble setting UIButton title of selected Action picker item, SWIFT - swift

I am finding trouble to set title for button of action picker item.
ActionSheetMultipleStringPicker.show(withTitle: "Select Nationality", rows: [
nationalityArray], initialSelection: [selectedNationalityIdStr],
doneBlock: {
picker, indexes, values in
let selectedEmirate = "\(values)"
self.btnNationality.setTitle(selectedEmirate, for: .normal)
print("values = \(values)")
print("indexes = \(finalIntId)")
print("picker = \(picker)")
return
}, cancel:
{
ActionMultipleStringCancelBlock in return
}, origin: sender)
the print of Values inside the Action sheet picker is this
values = Optional(<__NSSingleObjectArrayI 0x600001ba1740>(Expat)
so when I write this below line to set title for a UIButton then it is setting up whole
self.btnNationality.setTitle("\(selectedEmirate)", for: .normal)
As you can see here, it is taking whole values string instead of only "expat":
Some one help me setting up only "expat" from the values of my action sheet picker please

Related

Swift dropDown library

I am using the dropdown library. When you press a button, you want to develop it in a format where text changes on the button when you click an element.I can print but I don't know how to change the text of the button. And I want to change the sort order of the table view when I click that element. How should I do it?
func dropDown() {
sequenceDropDown.dataSource = ["Latest order
", "Priority"]
sequenceDropDown.show()
sequenceDropDown.anchorView = sequence
sequenceDropDown.bottomOffset = CGPoint(x: 0, y:(sequenceDropDown.anchorView?.plainView.bounds.height)!)
sequenceDropDown.selectedTextColor = .white
sequenceDropDown.selectionBackgroundColor = .mainColor
}
This is dropDown function.
output.loadData.bind(to: tableView.rx.items(cellIdentifier: "mainCell", cellType: MainCell.self)) { row, element, cell in
cell.nameLbl.text = element.nickname
cell.titleTxtField.text = element.title
cell.contentTxtView.text = element.description
cell.postImage.image = UIImage(named: element.media)
cell.timeLbl.text = element.date
cell.sirenBtn.rx.tap.subscribe(onNext: { _ in
self.selectIndexPath.accept(row)
}).disposed(by: self.disposeBag)
}.disposed(by: disposeBag)
This is binding tableview code.
setting button's title is easy myButton.setTitle("Play", for: .normal)
if you want to change sort of table view, you probably should map loadData observable to needed sort method, maybe it's even better to store ouput from loadData to some array and wrap it to observable or subject, bind it to table view and if you need to see sorted tableview just sort the array
(maybe I didn't get you case point completely)

Resigning textfield by clicking on other textfield in swift

i have three text fields. one is for showing date picker, second is to showing options for gender and another one is for showing country code. let say gender dropdown box is showing now, if i will click on dateofbirth or contrycode textfield, then gender will be hidden. like that if date of birth is showing and i will click gender or country code textfield , then date of birth textfield will be hidden. same will work for countrycode.
code for showing gender textfield:-
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField == gender_TextField{
tableViewOne.snp.updateConstraints { (make) in
make.top.equalTo(textField.frame.maxY)
make.left.equalTo(textField.frame.origin.x)
make.right.equalTo(0-(self.containerView.frame.maxX-textField.frame.maxX))
make.height.equalTo(0)
}
self.containerView.layoutIfNeeded()
UIView .animate(withDuration: 0.4, animations: {
self.tableViewOne.snp.updateConstraints({ (make) -> Void in
make.height.equalTo(130)
})
self.containerView.layoutIfNeeded()
}, completion: nil)
if(textField == gender_TextField){
self.selectedTextfeild = 1
self.selectedTextFieldType = DROPDOWN_TEXTFIELD.gender
}
tableViewOne.tableFooterView = UIView()
tableViewOne.reloadData()
tableViewOne.isHidden = !isTableViewHiden
isTableViewHiden = !isTableViewHiden
return false
}
return true
}
code for showing date picker
datePickerView.addTarget(self, action: #selector(dateChanged(datePicker:)), for: .valueChanged)
datePickerView.datePickerMode = .date
dob_TextField.inputView = datePickerView
datePickerView.maximumDate = Date()
code for showing countrycode
countryPickerView.delegate = self
if countryPickerView.selectedCountry.phoneCode != "+93"{
countryCode_Lable.text = countryPickerView.selectedCountry.phoneCode
}
else{
countryCode_Lable.text = "+1"
}
You can hide the other two views and can resign the other two textFields when one is active.

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

How can I reset navigation bar items when coming back from popup?

I have a navigation bar with two buttons as the right bar button items and a text field in the title view. If I tap the text field, a search screen pops up and I can enter texts into the text field. The texts in the text field would set the "resultText" variable in my code below. The button items, including filterItem and mapItem, are well connected with #IBOutlet.
I would like to hide the right bar button items when the text field is not empty. With the code shown below, it works fine initially when I enter texts into the text field. However, when I delete the texts in the text field and then returns from the pop-up, the app crashes because the button items are found nil. I do not understand why it is nil. Am I missing something here?
if !resultText.isEmpty {
navigationItem.rightBarButtonItem = nil
} else {
navigationItem.setRightBarButtonItems([filterItem, mapItem], animated: false)
}
You are adding and removing buttons from the navigation bar, it must be removing reference from view. Try adding it using code -
func addBarButtonItems() {
let filterItemBarButton = UIBarButtonItem(title: "filterItem", style: .plain, target: self, action: #selector(filterItemTapped))
let mapItemBarButton = UIBarButtonItem(title: "mapItem", style: .plain, target: self, action: #selector(mapItemTapped))
navigationItem.rightBarButtonItems = [filterItemTapped, mapItemTapped]
}
func removeBarButtonItems() {
navigationItem.rightBarButtonItems = nil
}
#objc private func filterItemTapped() {
//code
}
#objc private func mapItemTapped() {
//code
}
Call these methods correctly in textField delegate methods.

Refresh UIButton text with fade animation

I'm trying to implement a simple QRScanner app that presents a button, if a QR code is scanned. Now I want to present the same button for multiple QR codes, but in case two QR codes are scanned one after the other, and both codes present the same button, I want to shortly refresh the button text with a fade animation in order to indicate that a new QR code has been scanned. I noticed that if you change the button title, that animation is triggered but if you set it to a string identical to its current title, it doesn't show any animation anymore. I was wondering if there's a simple way to trigger that same animation even if I don't want to change the title's value. Here's my code excerpt:
func setValidMessageLabel() {
messageLabel.backgroundColor = color
messageLabel.setTitle("Check Now", for: .normal)
messageLabel.setTitleColor(.white, for: .normal)
}
func setInvalidMessageLabel() {
messageLabel.backgroundColor = UIColor(white: 0.7, alpha: 0.6)
messageLabel.setTitle("Invalid Code", for: .normal)
messageLabel.setTitleColor(.white, for: .normal)
}
if metadataObj.stringValue != nil && metadataObj.stringValue != key {
key = metadataObj.stringValue
messageLabel.isEnabled = true
switch key {
case cokeWin:
setValidMessageLabel()
case cokeLose:
setValidMessageLabel()
default:
setInvalidMessageLabel()
return
}
}
Happy for any help :)