I have a UISegmentControl where I'd like if none of the segments are selected when the segment first loads.
Is this possible or do I need to write some custom control?
Many Thanks,
-Code
Just uncheck the 'selected' checkbox for each segment in the Interface Builder Inspector.
EDIT:
If you are doing it through code, this should happen by default. See the discussion for selectedSegmentIndex -
// ignored in momentary mode. returns last segment pressed. default is UISegmentedControlNoSegment until a segment is pressed
// the UIControlEventValueChanged action is invoked when the segment changes via a user event. set to UISegmentedControlNoSegment to turn off selection
#property(nonatomic) NSInteger selectedSegmentIndex;
value of UISegmentedControlNoSegment is -1. So no segment will be selected by default
Related
I have many controls like, image views, labels (UIControls), which have I wish to show like a dual mode controls. i.e. Based on my data, I have to set them either with image 1 or image 2 (for a image view), similarly with the label. I tried accomplishing this using the highlighted state properties of image view and labels. For the image view, I gave one image reference for highlighted and another for normal.
however when I programmatically set the highlighted property to yes, they are not toggling between them. Is there something I'm missing?
From the documentation:
Highlighted state of a control. A control enters this state when a
touch enters and exits during tracking and when there is a touch up
event. You can retrieve and set this value through the highlighted
property.
So, you don't set the highlighted property. Try setSelected.
Have you seen this related topic: Highlighting a UIControl subclass?
Use selected state instead. I think highlighted state is a transient state.
Try this out:
if([imgeview isselected]){
[imageview setselected:NO];
}else{
[imageview setselected:YES];
}
I am trying to set a state for UIButton.
But i don't know the difference between the UIControlStateHighlighted and UIControlStateSelected.
Could anyone help me out?
Thanks and best regards.
They can mean whatever you want them to, but in general they mean the following:
Highlighted = The user is currently interacting with something that will change once they stop interacting (e.g. holding down a button)
Selected = The item is current the active item in a group (e.g. The selected item in a segmented control). This can only be achieved by setting it programmatically.
UIControlStateHighlighted = it highlights the button with some flash(in button background) when the user taps.
UIControlStateSelected = it highlights nothing to that button.
From the official doc:
UIControlStateHighlighted Highlighted state of a control. A control
enters this state when a touch enters and exits during tracking and
when there is a touch up event. You can retrieve and set this value
through the highlighted property.
UIControlStateSelected Selected state of a control. For many controls,
this state has no effect on behavior or appearance. But other
subclasses (for example, the UISegmentedControl class) may have
different appearance depending on their selected state. You can
retrieve and set this value through the selected property.
Your button get highlighted in reaction of a touch event. It could then be on a selected state within a group (for segmented control).
Highlighted is typically applied transiently when the control is being touched, selected is a more permanent state. Imagine a checkbox type button which dimmed while it was being touched - dimming is highlighted, ticked is selected, unticked is unselected.
Typically you'd never set highlighted status manually as the system will be setting/unsettling it in response to touches, whereas selected is safer. This particularly applies to buttons.
I am using SSToolkit and SSCollectionView. When I click a SSCollectionViewItem, I would like to darken the item so the user knows it has been pressed.
The documentation (http://sstoolk.it/documentation/Classes/SSCollectionViewItem.html) shows a setSelected and setHighlighted method, but I am not sure how then to change the appearance of my item.
Any help?
The answer is in the documentation:
Discussion
Highlights or unhighlights the item,
animating the transition between
regular and highlighted state if
animated is YES. Highlighting affects
the appearance of the items's labels,
image, and background.
Note that for highlighting to work properly, you must fetch the item's
label (or labels) using the textLabel
(and detailTextLabel) properties and
set the label's highlightedTextColor
property; for images, get the items's
image using the imageView property and
set the UIImageView object's
highlightedImage property.
A custom table item may override this
method to make any transitory
appearance changes.
SSCollectionViewItem
I'm setting the selectedSegmentIndex of an UISegmentedControl via code.
Whenever I do this the valueChanged action gets called. This sounds logical for me, but is there a way to set the selected segment without invoking the action? It should just update the display.
I've used UISegmentedControl more than once, and until now I didn't even noticed that behavior. But this time I need to present an alert if a special segment is selected. So I can't live with the invoked action if the view appears and I want to show the previous selected value.
I could disconnect the action, change the selectedValue and reconnect the action. But maybe there is a better way.
Your question is a sensible one; I can only suggest that you've found a bug, since this is so unusual. Usually, changing a control in code doesn't cause any control events to be emitted. Setting a UIDatePicker's date doesn't emit a Value Changed event. Setting a UIPageControl's currentPage doesn't emit a Value Changed event. Setting a UISlider's value doesn't emit a Value Changed event. Setting a UISwitch's on doesn't emit a Value Changed event. Similarly, setting a UITextField's text doesn't emit an Editing Changed event. So, the fact that changing a UISegmentedControl's selectedSegmentIndex emits a Value Changed event feels wrong. I'm going to file a bug and I think you should too.
I don't see an obvious way to determine whether the Value Changed event was triggered by the user tapping or programmatically. You'll have to raise and lower a BOOL flag or something.
EDIT: This is fixed in iOS 5.
I still have this issue in iOS 7.1.
I was reloading table and setting segment index but it was calling 'value changed' delegate.
To avoid that I nilled 'segment control's' delegate first and then set value programatically, and again reset the delegate to current class. This won't call 'value change' delegate.
As per Apple
The selectedSegmentIndex property is generally used to determine:
the index number for identifying the selected segment (that is, the last segment touched).
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.
You can also change it to different value depend upon your requirement.
UISegmentedControl ignores this property when the control is in momentary mode. When the user touches a segment to change the selection, the control event UIControlEventValueChanged is generated; if the segmented control is set up to respond to this control event, it sends a action message to its target.
(ref)
hi i have used UIButton and also UIpickerview,when i select value(select team) ,the value is changed(india).but
when i hold the button(india) ,the value is changed to initial value(select team).if i release the button from pressing ,the value is changed(india).but i want to do when i hold , the button must show (india) ...i want to use selected state configuration...anyhelp?
What is value here? UIPicker's value? or it's some other control in your view?
Check out Highlighted state for UIButton in Interface Builder... hope this helps!