Deleselect uisegmentedcontrol selection after clicking on a save button - ios

I'm using a 3 button UISegmented control for a choice selection. I also have a save button that retrieves the chosen control.
When the save button is clicked I want to have to UISegmentedcontrol cleared (ie the previous selected button unselected). I'm not looking for the setMomentary as I want the selection to stick but also be able to unselect it later.

[myUISegmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];

myUISegmentedControl.selectedSegmentIndex = -1; //turn off the current selection

With Swift 4 and iOS 11, the Apple documentation states for selectedSegmentIndex:
The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.
Therefore you can use one of the two following implementations in order to remove the selection of your UISegmentedControl instance:
mySegmentedControl.selectedSegmentIndex = -1
mySegmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment

Related

Accessibility Label for radio button on action?

In TableViewCell, I have custom imageview which act as a radio button. This imageview has two states
Selected
Not Selected
By default I have given, Accessibility label as "Selected Checkbox" and "checkbox". Now I want to speak voice text as "new item selected" when it selects and "item deselected" when deselected.
Can we give all four different label? How can I get the same.
Updated: I tried using
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification,#"text")
but it skips the text which need to speak.
I'm not entirely sure what you are attempting to do here but I'll try to provide an alternative way of making this accessible.
Instead of treating each radio button as either selected or deselected would it not be better to treat the entire group of radio buttons as one combined accessibility element.
So if you have radio buttons for [cat, dog, rabbit, guinea pig]. Then your accessibility should read something like...
Animal selection group: none selected
or
Animal selection group: rabbit selected
etc...
Having said all of that... what is this UI you are trying to create?
Radio buttons are fundamentally not a part of iOS. It would be a much better alternative to use UI that users know. And then by doing this you make your accessibility issue a non-issue.
Perhaps a UIPickerView or a UITableView might be better alternatives to radio buttons?
Not sure what are you exactly want to do.
My assumption: You want to generate sound on click of a button for tableviewcell and also changing the image of Imageview at the same time while checking the state of it (selected or non-selected).
***** do it inside your customTableViewCell class...
create IBOutlets in .h or .m for your 3 items from customtableviewcell xib.
create boolean flag to maintain current state (selected or non selected) of that cell.
create IBaction selector(method) to get event(touch up inside) of button click. Inside this method write code which checks following.
BOOL selectState; //Make this Global in classfile
if(selectState) // selected state YES
{
xyzImgView.image = //Your Non selection Image;
selectState = !selectState;
//Play your sound for Non selection
}
else // selected state NO
{
xyzImgView.image = //Your selection Image;
selectState = !selectState;
//Play your sound for selection
}
// Considering you have an IBOutlet to checkBoxButton
checkBoxButton.accessibilityLabel = checkBoxButton.isSelected ? "selected" : "not selected"
checkBoxButton.accessibilityHint = checkBoxButton.isSelected ? "" : "Tap to select"

Why UIButton can't stay selected state when disable?

I have a UIButton which is set different images for normal and selected state.
I find that when I disable the button by setting button.enable = NO, even previous is selected state, the button will turn to normal state image, But I po the button selected value which is still YES.
How can I keep using selected image when button is disable? Is that the only way by changing normal state image when button change enable value?
Set the image for the case when button is Selected and Disabled as this is a different state that the solely other state. Set the image but for state like this:
UIControlStateSelected | UIControlStateDisabled
Control's state can be a combination of a few states from the list :)
As a nice lesson you can log changes of a control's state and see combinations of states it will gets in different cases.
In Swift:
let state = UIControl.State.selected.union(UIControl.State.disabled)

UISegmentControl Customisation

I want to customise UISegmentControl according to link of the screenshot(Shows is selected and News is unselected) given below:
UISegmentControl Customisation
Segment with Title and Image
Borderless segments
Tap event even if Segment(Shows) is selected - I want to perform some action if user tap on selected segment
[SegmentedControlName setImage:[UIImage imageNamed:#"path"] forSegmentAtIndex:0];
For selected event you can use same thing when it also on the touched state.
Only you need to change is the path of the image
I ended up using the following library. It fulfilled my all the requirements.
PPiFlatSegmentedControl

Detect second click on a segment

According to the documentation, the event which should related to UISegmentedControl is value changed. Assuming I have a segmented control with previous and next, in my case I should be able to click next more than one time, the default behaviour of UISegmentedControl will not recognize the successif second click on same segment. SO how to deal with that?
Set the momentary property of your UISegmentedControl to TRUE.
You can do that in code or in Interface Builder (there is a checkbox in the Attributes Inspector).

How to deselect selected text in an iPad application

I have a custom menu item (UIMenuItem) attached to a UIWebView. When the user selects my custom menu item after selecting some text in the web view, I want to "deslect" the current text selection. The problem is that the edit menu will pop up again when I show a modal form for some reason, I think because there is still a selected (highlighted) text range. I want to programmatically "unselect" the selected text in my custom menu item handler, after I've captured the selected range.
Use UIView's userInteractionEnabled property. Just disable user interaction and enable it back.
Like this:
myWebView.userInteractionEnabled = NO;
myWebView.userInteractionEnabled = YES;
A cleaner solution:
Swift
webView.endEditing(true)
Objective-C
[webView endEditing:YES]

Resources