How to set localized show/hide buttons in NSOutlineView - objective-c

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.

Related

is single radio button available in cocoa?

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.

How to disable context menus with right mouse click in an NSTextField (Cocoa)?

I'm working on a Cocoa application that has editable text fields. These text fields need to accept values but don't need to be spell checked or use any of the other options given in the default context menu. I've read that the easiest way to remove the right click/ opt + click context menu is to override the function:
rightMouseDown:(NSEvent *)
I've done this in a custom NSTextfield class. This fix blocks the user from right clicking when the text box is enabled and unselected, but as soon as the user double clicks/enters the text field for editing the default right click functionality returns.
Is this because the firstResponder switches to a class in the inheritance chain upon trying to edit the field? Is this approach the right way to disable all context menu functionality for this NSTextField?
Thanks!
When a text field is editing, the actual first responder is the "field editor", an NSTextView supplied by the window. The field editor always uses the control on whose behalf it is acting as its delegate.
So, in order to influence its behavior with respect to the contextual menu, you need to use a custom subclass of NSTextField. (I guess you already are to override -rightMouseDown:.) Then, implement the text view delegate method -textView:menu:forEvent:atIndex: and return nil (i.e. no menu).

Get the representedObject values of NSCollectionViewItem NSButton click

I have read some questions and I find some very confusing and I don't really know if they answer my question.
I have an NSCollectionView implemented and connected to a Core Data context, everything shows correctly.
Now what I have is buttons in the view prototype, and when I click this buttons I need to get the value of the representedObject of that cloned view.
I have read and read and some parts are confusing to me, so I'm looking for a simple explanation.
Thank you for your time.
An action method takes one argument:
- (IBAction) collectionViewButtonClicked:(id)sender {
}
That sender is the control or other UI element (e.g., menu item) that sent the message.
With that argument, when your action method gets called, you know which button was clicked.
A button is a kind of control, and every control is backed by at least one cell. Cells have represented objects, too.
So, first, set the represented object of your button's cell to the collection view item that owns the button. (You can do this in the nib editor.) Then, in your action method, get the button's cell, then the cell's represented object (which is the item), then the item's represented object.
If the representedObject outlet doesn't show up in the nib editor, you probably have the button selected, not its cell. I recommend opening the nib editor's outline view using the button in the lower-left and then never, ever closing it.

NSOutlineView select with the first click

I have an NSPanel with a NSOutlineView to display my data. I want to select a row with a first click inside the NSOutlineView even if the panel is not in focus (i.e. not the key window) I have a delegate that allows selection and I have overridden the NSOutlineView class to override the acceptsFirstMouse: method, but I could not get the first click selection.
I checked the mouseDown event and it fired without any problems (in my class that overrides) when the panel was and wasn't the key window. But when the panel is not key, NSOutlineViewDelegate method shouldSelectItem: is not called.
What am I missing/doing wrong?
You just need to subclass your NSTableOutlineView and override:
- (BOOL)needsPanelToBecomeKey
to return NO.
NSView class reference states:
Overridden by subclasses to determine if the receiver requires its
panel, which might otherwise avoid becoming key, to become the key
window so that it can handle keyboard input and navigation.
Discussion
Such a subclass should also override acceptsFirstResponder
to return YES.
This method is also used in keyboard navigation. It determines if a
mouse click should give focus to a view (make it first responder).
Some views will want to get keyboard focus when you click in them, for
example text fields. Other views should only get focus if you tab to
them, for example, buttons. You wouldn't want focus to shift from a
textfield that has editing in progress simply because you clicked on a
check box.
Source: Apple documentation

Bold + Italic Shortcuts in Document-Based Applications

I've been reading into using the First Responder as a proxy object, and dragging connections from buttons in order to create usable actions (align left, right, etc). However, by default, the First Responder doesn't contain Bold and Italic actions. I tried adding these actions by opening my Menu.xib and dragging the (Font > Bold) menu item to the first responder and setting it as a boldIt function (custom) but that overwrites the existing action (Font Manager: Addtrait). Is there a way to somewhat efficiently add a bold/italic button on the document.xib? Seems like a lot of work for something so common. New to Obj-C
Best,
Zach
In the menu xib, there is an object (the Bold menu item), whose tag is 2, and whose action is connected to the -addFontTrait: method of an NSFontManager instance.
You simply need to do the same thing in document xib. Add an NSFontManager object; create your Bold button; set its tag to 2; and connect its action to -addFontTrait on the NSFontManager.