Swift Radio Buttons - CheckBoxes - Swift 3 - ios

I work on Swift 3, and I'm trying to make checkboxes that behave like radio buttons. I explain myself : I have 8 buttons ; each has an "unchecked" image and a "checked" image. I want only 1 button to be selected (so, showing ITS "checked" image) at the time. The principle of radio buttons.
So I tried using the #IBAction with each button, I've this :
#IBAction func tona1_changed(_ sender: Any) {
if test_tona1.isSelected == true
{
test_tona1.setBackgroundImage(UIImage(named: "0b_unchecked"), for: UIControlState.normal)
test_tona1.isSelected = false
}
else
{
test_tona1.setBackgroundImage(UIImage(named: "0b_checked"), for: UIControlState.normal)
test_tona1.isSelected = true
}}
It actually make the image switch between "uncheck" and "check" but I don't know at all how to make it interact with the other buttons.
I tried using an array with the buttons's tags inside but I didn't have something working. I also tried to make a class but I don't know how it can work.
Thanks!

You can achieve that in several ways. One of the most trivial would be to:
Have a datasource. Most of the time an array.
Create an UIButton for each item in your datasource, and insert each button into another array. Each button would have a tag corresponding at the
index of the array. Also set different images for selected state and normal.
Add an action for each button, with the target function being something like:
func buttonPressed(sender:UIButton) {
for button in buttons {
button.isSelected = false
// deselect your model datasource[button.tag]
}
sender.isSelected = true
// select your model datasource[button.tag]
}
I leave the abstraction / improvements / safety to you. The key point is just to use a collection of buttons, iterate through them, and select / deselect them accordingly, and not using a big if/else checking for the button tag everytime.

Related

How to hide table cell after choosing a drop down in swift 5?

I have a problem here..
So, I am using cocoapods for dropdown and table view controller,
and I want to hide the next cell if the dropdown says "Yes", and keep the next cell
when I hit " No ".
How do I do that?
How do I hide the next section?
Here my code & visual image.
Any help will be appreciated.
Thankyou
#IBAction func btnTapped(_ sender: Any) {
DropDown.show()
let choosen = DropDown.indexForSelectedRow
if choosen == 0 {
self.section2.isHidden = true
}
else if choosen == 1 {
self.section2.isHidden = false
}
}
Your dropdown function in getting called on button tap. You should use the delegate method of your dropdown function to know once user has chosen a value. Then inside that function you can hide unhide your cells.

Setting a button image depending on another variable? Swift

so I'm very new to swift. I currently have 16 buttons all set to individual outlets box1,box2,box3 etc.
Each box I have set a tag and what I am trying to do is set the image of a particular box based off another integer variable to determine which box I'm changing.
So say I do a calculation and index = 4.
Is there a way I can then set box(index).setImage?
I understand this probably isn't the best way to do it or even possible, maybe I can set each button to an array of objects instead? Any tips would be great.
If you want to set the image of button through some tag then you don't need any outlets for the buttons.
You can change your button image by finding the button through viewWithTag() property.
Here is the code
var button = self.view.viewWithTag(Your_Calculated_Index) as! UIButton
button.setImage(Your_Image, for: .normal)
You can try this. You need to create an array for the button.
#IBOutlet var allbtns: [UIButton]!
for buttons in allbtns{
if buttons.tag == 4{
print("Button 4 ");
buttons.setImage(UIImage(named: "imagname"), for: UIControlState.normal)
}else{
print("Other buttons except 4 ");
}
}

iOS: Good way to pass value from image button to func in the class

Initially I had two buttons with titles: "+" and "-". Both buttons are located in table cell. I have used protocol and delegate to pass value from button title to function call which depends on input params. If input parameter is "-" - decrease value, if "+" - increase.
But later I removed titles and replaced buttons with respective images. And here I faced an issue - I cannot call function properly because title is blank.
I have implemented the following workaround. I have set Accessibility Identifier for both buttons. For example for +:
And in cell class I used accessibilityIdentifier:
#IBAction func didPressOrderCellButton(_ sender: UIButton) {
cellDelegate?.didPressOrderCellButton(order: order!, menuItem: menuItem!, action: sender.accessibilityIdentifier!)
}
But... I have some doubts if it's proper way to do this. Will it create any issues in future if I decide to work with accessibility feature?
I do not want to use title and add code to ViewController class to hide it every time view is loaded or appeared. I want to avoid such solutions in my code because it's too difficult to support such solutions I believe.
I have tried to find another button identifier that I can use, but didn't succeed.
The simple approach would be just set the tag value inside your button and then check the same tag value and perform your operation Plus or Minus accordingly.
For instance:-
if sender.tag == 0 {
//Do minus
} else if sender.tag == 1 {
//Do plus
}

Touch Up Inside not working properly

I have an app with some buttons, when those buttons are pressed the image on them should change. I assume that the TouchUpInside runs when you tap and remove the finger while still holding inside the area of the element, however it only works rarely and I'm not sure why.
The reason I use TouchUpInside instead of TouchDown is because I want the user to be able to cancel the action.
I'm sorry if I've misunderstood anything about those events and if this has already been asked. I couldn't find an answer to my problem searching the web.
//The IBAction is set to trigger on TouchUpInside
#IBAction func action11(sender: UIButton) {
setTile(sender)
}
func setTile(sender: UIButton) {
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
}
EDIT: Added the necessary code
There are some properties of UIButtons which you can use to achieve what you want.
You can use Default and selected state of uibutton to set two different images.
In XIB select state "Default" and assign default image to that state again select state to "Selected" and assign image which you want after button section.
and add following line in button selection method.
-(IBAction)buttonTapped:(UIButton *)sender{
sender.selected = !sender.selected;
}
Your understanding is correct, you need to use touchUpInside.
I assume you are trying to create a button that has a toggle function. On one touch you want the button to have the value Say "X" and when touched again the button has a value "O".
Take a look at this code below, this should do the job.
class ViewController: UIViewController {
var isButtonPressed = false{
// Adding a Property Observer, that reacts to changes in button state
didSet{
if isButtonPressed{
// Set the Value to X.
}else{
// Set the Value to O.
}
}
}
#IBAction func changeButtonValue(sender: UIButton) {
// Toggle the button value.
isButtonPressed = !isButtonPressed
}
}
If you don't set turn=true after the first time, this code is executed it will be executed only one.
if turn {
print("O's turn")
sender.setImage(xTile, forState: .Normal)
turn = false
}
Check if the button frame is large enough to get finger touch.
Apple says at least 35x35 pixel.

Comparing strings within buttons in swift

I'm implementing a favourite button in swift for my quote application. Since I want to disable the user to be able to favourite a quote twice. I must change the text to unlike and then compare the button text mode being unlike to the button text mode being like. And then do furthermore things based on these conditions. It would look something like:
#IBAction func favour(sender: AnyObject) {
if liketext.text == "Like"{
liketext.setTitle("Unlike", forState: UIControlState.Normal)
makeQuoteFavourite()
} else if liketext.text == "Unlike" {
liketext.setTitle("Like", forState: UIControlState.Normal)
}
}
However as many of you know, a button outlet cannot have the .text function. #IBOutlet var liketext: UIButton! How would I be able to compare button strings to normal strings? Are there other possible solutions?
UIButtons simply work differently than UILabels. They have control states for one thing. And their (title) text can be set on a per-control-state basis.
So you have to use the currentTitle property:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/currentTitle
or the titleForState method:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/titleForState:
Now those titles are normal String objects; it's the name of the property that's different.

Resources