I am trying to drag-drop a single radio button from object library in cocoa. But there is "radio group" object is available to drag-drop. In radio group has two radio button.
Is there any way to create a single radio button in cocoa application or hide the one radio button from radio group?
Thanks
You can make it one by going to attribute inspector and set the cell count to 1. but you have to handle the behaviour by your self logically.
Single radio button is not available in object library in cocoa. You can just use a normal UIButton and give its normal and selected images in Interface Builder.
Alternatively, you can just use a custom library for radio button like this one : https://github.com/onegray/RadioButton-ios
You should use a simple UIButton.
You can set different texts/images depending on a button status, like Devanshi suggested. The if/else approach is a bad idea though, because cocoa already provides these and are much more simple and small.
There are two ways, either storyboard (if you use it) or programatically, wherever you create your button (most likely viewDidLoad).
If you are using Storyboard
Storyboard is pretty straightforward, select your button, and under the "State" you can chose different settings. Each settings will actually load different parameters for the next settings. Like the title and image for example.
Just go on state "default" and set an unchecked box as image.
(You can)go on state "Highlighted" and set a temporary highlighted box as image; this is not mandatory.
Then go on state "Selected" and set a checked box as image.
And you're done.
If you are using only code
The programatic way to do this is fairly simple.
I assume we have a button called btSend,
UIControlStateNormal, UIControlStateSelected, UIControlStateDisabled, UIControlStateHighligted, are the states you can use. I used disabled and normal (enabled) here.
[self.btSend setImage:[UIImage imageNamed:#"SendIcon.png"] forState:UIControlStateNormal];
[self.btSend setImage:[UIImage imageNamed:#"SendIconGrayed.png"] forState:UIControlStateDisabled];
With this, you'll be up and running too.
After setting that up
Now that your buttons know what to show depending on their state, you need to add a selector method for your action (an IBAction), in which you will need to invert the state of your current button. You probably already have that, just add a boolean.
checkBoxSelected is a boolean that I created as an instance variable (on top of .m file). You can set it to "NO" in viewDidLoad if your radio is not selected by default, and to YES if it is selected by default.
#implementation TermViewController{
BOOL checkBoxSelected;
}
here is the method that is called when I press the button
- (IBAction)tapRadioButton:(id)sender {
checkBoxSelected = !checkBoxSelected;
[_btCheckbox setSelected:checkBoxSelected];
}
You probably have "something" that checks if your checkbox is selected, to know that, you can either get the state of your button by using self.yourButton.state. This will return a UIControlState, just check if it's Normal or Selected or Disabled or HIghlighted.
Or you can check the Boolean "checkboxselected" if it's YES or NO :)
If you have many radio buttons, you can save all those boolean values in a dictionary. the key would be your button number for example, and the object would be YES or NO. if you press the button, (like shown before), change the button state and also change it in the dictionary. At the end you have all your states in the dictionary.
I didn't explain all that in my first writing because that wasn't really a part of your question :) I hope this helps.
For radio button can use simple UIButton and maintain state of it and replace the image of on click event.
1) Declare globally in view
BOOL isChecked;
2) On button click event
if (isChecked)
{
[_btnRadioBox setImage:[UIImage imageNamed:#"chk_uncheck.png"] forState:UIControlStateNormal];
}
else
{
[_btnRadioBox setImage:[UIImage imageNamed:#"chk_check.png"] forState:UIControlStateNormal];
}
isChecked=!isChecked;
3) And get the status of it.
if (isChecked)
{
// Here write code of radio button when selected
}
else
{
// Here write code of radio button when unselected
}
You can use MVRadioButton to add animated radio button in iOS. This is very simple Drop in and easy to integrate Custom Control.
Its using CoreAnimation and Layers to Give it look and feel exactly like a Traditional Radio button that we have seen on Web.
Related
I'm trying to group 2 radio buttons in cocoa application.
but unlike in iOS, where you can connect the buttons with control-drag
(as shown in the picture)
in cocoa application I didn't find any elegant way to do so...
any suggesting before I do something ugly?
NSMatrix used to be the solution, but it is now discouraged:
NOTE: Use of NSMatrix is discouraged in apps that run in OS X v10.8
and later. If you need to create a radio button group in an app that
runs in OS X v10.8 and later, create instances of NSButton that each
specify a button type of NSRadioButton and specify the same action and
the same superview for each button in the group.
If all buttons call the same action method and are in the same supperview, Cocoa automatically selects the clicked button and deselects the previous button and -- no code necessary to do that.
I found this (maybe not elegant):
control-drag a single radio button to a create an sent-action.
control-drag the other button(s) to the same (empty) function.
You have to hover over the function to get it connected.
Now they are grouped.
so here is my not very elegant solution -
define array of buttons in the class:
NSArray* _radioButtonsArray;
initialize it in viewDIdLoad:
_radioButtonsArray = [[NSArray alloc] initWithObjects:_radioButton1,_radioButton2,_radioButton3, nil];
define a radioButtonIsPressed method and connect it to all radio buttons as an action:
-(IBAction)radioButtonIsPressed:(id)sender{
for (NSButton* btn in _radioButtonsArray){
if (btn != sender)
[btn setState:0];
}
}
I control-drag the single radio button to create an IBAction. Then I copy/past the radio button onto the same view. Now there are two with the same IBAction. Cocoa treats them as grouped.
What is the point of ever setting a button as an outlet? I am following a tutorial and the teacher didn't really mention why he set a button as an outlet. A button is suppose to do an action/call a method and so we set it as an IBAction.
He sets the button as an outlet and then proceeds to change the text of the button through Xcode in viewDidLoad, but why not just keep it as an IBAction and change the text by using setTitle: forState:UIControlStateNormal ?
Isn't a button suppose to cause an action by definition?
in some logic cases you would need to change the behaviour of the button , e.g. upon invoking an action (triggered by other event )you will need to disable it or change its backgroundColor or text.
you don't have to set at all times , but in many cases it is really useful
I have a colorwell that when clicked fires to an IBAction method. There I check to see if the new color selected is different then the default color for an event and if it is, I show a sheet dialog to the user alerting them to this.
The problem I am having is that the color picker calls the action method every time a control such as the slider is moved. This causes the action method to be called n times instead of just once, and I have to respond to the dialog n times.
In IB there is a checkbox for continuous state. If I leave it unchecked it does't call the action method at all. Most slider controls allow you to choose between continuous state or a single state, but I am not seeing this option for a color well.
Any advise appreciated;
Simple Code:
-(IBAction)colorwellManager{
if([self shouldAlertUser] == YES){
[self dialog:#"Your are about to change the default color" #"Confirm Button"];
}
}
If a slider is moved on the color picker, this code executes many times.
I found I good explanation for whats happening here:
NSColorPanel blocking mouse up events (second answer)
The underlying class (NSColorPanel) needs to have it's setContinuous set to NO programmatically in addition to unchecking the colorwell's continuous state checkbox in IB. This allows the color well to call the action method only once per action.
I have a static NSTextField that overlays a large error message in my OS X app. I'm trying to get it to allow the user to click controls beneath it.
In IB I've unchecked "enabled" and I've checked "Refuses First Responder"
I've also done it in code because that wasn't working:
[largeErrorText setEnabled:NO];
[largeErrorText setRefusesFirstResponder:YES];
Still, it is getting in the way of interacting with the objects below it. Any ideas what else it might be?
The only way I have found to make an object transparent to the click is to subclass that object (in your case the NSTextField) and override the hitTest method returning nil. This way that NSTextField will not respond to the click so the NSView below will respond to the click.
- (NSView*)hitTest:(NSPoint)aPoint
{
return nil;
}
I assume you are describing a scenario like the following image shows:
The inner red rectangle is the frame outline of the NSTextField label, and you're saying that even though you've disabled the text field and set refuses first responder, your clicks do not go through to the NSButton?
This design scenario describes a condition called "Overlapping sibling views". I would generally try to avoid this if at all possible. If you can't, you can get the desired behavior by making sure that the NSTextField label is "behind" all of the other UI objects that you want to be able to interact with. You can do that by selecting the label and choosing Editor > Arrange > Send to Back. That will assure that the button is in front of the text field so that it can properly intercept mouse events.
I have NSOutlineView in my App. This function
-(BOOL)outlineView:(NSOutlineView*)outlineView isGroupItem:(id)item
sets some of items in outlineView as a group root(if function returns YES) + adds show/hide buttons at the end of cell to expand/collapse content of this group, but this button written in English. I'm from Belarus, and that is why I want show/hide words written in my language. Finder writes in my language, that is why i think, what there is some option to set it localized style for it.
How I can do this?
SOLVED:
Mac OS does this itself at the time of choosing localization of .nib file what contains NSOutineView
The official way to retrieve the localized show/hide button (as well as the disclosure button) is now documented in the NSOutlineView Class Reference.
let showHideButton = outlineView.makeViewWithIdentifier(NSOutlineViewShowHideButtonKey, owner: outlineView.delegate()) as? NSButton
It's important to note the state property of the button controls the Hide/Show title, which by default is not synced to the state of the NSOutlineView:
NSOnState = "Hide"
NSOffState = "Show"
NSOutlineViewDisclosureButtonKey
The normal triangle disclosure button.
NSOutlineViewShowHideButtonKey
The Show/Hide button.
The outline view creates these buttons by calling its inherited
makeViewWithIdentifier:owner: method, passing in the key as the
identifier and the delegate as the owner.
These keys are backwards compatible to OS X v10.7, however, the
symbol is not exported prior to v10.9 and the string value
(#"NSOutlineViewDisclosureButtonKey") must be used.