Unable to disable Checkbox (NSButton) inside a disabled NSTableView - objective-c

I have an app that contains an NSTableView instance with 2 columns. One column, is an instance of this:
#import <Cocoa/Cocoa.h>
#class NSTableView;
#interface MMCheckboxTableCellView : NSTableCellView
#property (weak) IBOutlet NSButton *checkboxField;
#end
and the implementation is all boilerplate with no custom code added at all. In code, the NSTableView instance sets tableview.enabled = YES (or NO) programmatically based on various state of the app. When tableview is disabled, the other column of the table, which is display only, stops responding to click actions, as expected. The checkbox column, above, continues to respond to clicks and lets the user check/uncheck the button instances.
To try to troubleshoot this, I added a Text Field (it's not hooked up to anything) in the the checkbox column and when the tableview is disabled, it isn't possible to enter text into the field. Is there something special about NSButton that I'm missing? Is it possible that because I derived this class from NSTableCellView with an added Check Box rather than using a Check Box Cell I'm running into this problem?

I finally figured this out. It appears that even though the NSButton is embedded in the NSTableView, setting enabled=NO on the table doesn't control the behavior of the button. The only thing that eventually made this work was to invoke setEnabled the the control based on the state of the app. This had to happen in the rendering of the cell, so when the state changes, it was also necessary to invoke NSTableView:reloadData so the visible cells would redraw immediately.

As an alternative, I decided to work around this same problem by setting up a computed property matching to my boolean property that returned either a blank or a checkmark character (✓). Then bound to this field as a normal NSTextfield for which the enabling/disabling works.
// Swift
// Computed variables on model for readonly display in table
var bIosText:String {return bIos ? "✓" : " " }
var bAndroidText:String {return bAndroid ? "✓" : " " }
Looks pretty good I think:
Of course this is only part of the solution, if you want to enable editing, you need to have multiple columns and hide/unhide as needed.

Do not embed an NSButton in the table view, only use a NSButtonCell. If your NSTableView was view based, it will switch to cell based (at least Xcode did that for me).
So this is wrong:
And this is right:
It should suffice to disable/enable the table view after this change, the button cell will behave accordingly.

Related

NSTextField Key Interface Control (KIC) behaviour

Background: I have a custom derivation of NSTextField that I am trying to use as a cell in a grid similar to a spreadsheet.
I have set the nextKeyView of my NSTextField and am confused between two states that the NSTextField can have BOTH of which involve it having the blue focus-ring drawn around it:
the cursor is blinking in it => in this case the TAB key behaves as expected and moves to the nextKeyView and it is in a "text accepting" mode
it has not quite "entered" the text field for the purposes of text editing => the TAB key does not respond with KIC behaviour and move to the nextKeyView and trying to enter any text just in fact results in the following:
-[NSEvent key]: unrecognized selector sent to instance 0x60800012bf40
From background reading and a bit of debugging it appears that the first state corresponds to control/focus being handed off to the window's shared text editor and the custom NSTextField actually loses firstResponder status in this process.
What's interesting is how this differs from the behaviour of two standard NSTextField controls dragged onto an NSView in Interface Builder. Running such a simple app only the first state above is present at all as you tab from one NSTextField to the other! I am glad to have the intermediate second state in my app (because I need it) but I don't understand why it is happening...
Summary: two related questions
How do I programmatically control the NSTextField entering these two states of effectively accepting text and not while it has the focus [I do not know how to achieve this at all so this is my main problem...]
How can I get the KIC behaviour in the situation where the NSTextField has the focus but is not in "text editing" mode since this is the behaviour I am trying to get. [My current conclusion is that KIC does not work in this situation and that standard practise would be to resort to the regular overriding of keyDown for the set of keys that would normally be associated with KIC.]

UITableView Cell - Edit?

Is it possible to use a UITableView to be able to enter a value into a database's field.
For example, if I was to have a UITableView pointing to a field within a database and if I wanted to enter a new entry into the database - tap on the UITableView Cell that would then allow keyboard input into the cell which ultimately end up being a new record in the database??
This is possible, but if something is possible doesn't mean you should be doing so.
You might ask why?
Well! you are trying to input data from view directly to database, this is a very bad practice. There are many reason for it being bad, the major is efficiency and security reasons.
You should consider using MVC pattern.
Now since its completely possible, I will explain the idea on how to do it and conclude with links that will have real code examples.
In tableView:cellForRowAtIndexPath:
add a TextField with tag (to get the reference back in future) and add it to contentView of the cell and have it hidden.
Now in tableView:didSelectRowAtIndexPath: make the cells editing property to YES.
Then, in tableView:willBeginEditingRowAtIndexPath:
get the reference to the textfield in contentview using viewWithTag: method and hide the textLabela and unhide the textfield.
In textfield's delegate textFieldDidEndEditing: make cell's editing property as no (yea, you need to keep the reference) unhide the textlabel and hide textfield.
In tableView:didEndEditingRowAtIndexPath: write methods which will commit the changes to your db.
Below are list of links which will get you code examples:
Having a UITextField in a UITableViewCell
Accessing UITextField in a custom UITableViewCell
iOS Database Tutorial
There are no examples for your requirement 'coz it bit bad way of doing things.
Yes its possible....
You can use delegate methods to take data form you cells textfield to your parent view controller and then save data in database.

NSTextField click-through?

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.

When should I use an IBOutlet instead of an IBAction?

I know the differences between IBAction and IBOutlet, but I don't know when I should use one over the other.
For example, I'm trying to make a calculator. I have ten buttons and one label to display a result. Must I create ten IBOutlets in my interface? If not, why not?
An IBOutlet is a connection to an object, and an IBAction is a connection to a method to be invoked as a callback. For example, you might have ten buttons and one label, but you might connect to the label using an IBOutlet (so you can update its value), and a single function - (IBAction)buttonPressed:(id)sender; that is connected to the onTouchUpInside event of each of the ten buttons. The buttonPressed: method's implementation will then need to inspect the sender to figure out what its value is.
You should not need to have an IBOutlet for each button if this is all you need to do.
Ok, IBOutlets and IBActions serve two separate purposes.
IBActions are basically methods that can be connected to UI elements through IB. They provide ways for your object to be notified when something has happened with the UI. They also provide the sender argument when they are called so that you can access the UI Element that sent the message from within the method.
An IBOutlet on the other hand offers a way to get a reference to the UI element within your code at any point, it is used when you need to change aspects of the UI.
For your situation you don't really need to have IBOutlets for the buttons because you don't need to change anything about them, you just need to be notified when they have been pressed.
As a note, if you have that many buttons, and you for some reason needed a way to access them from within your code to change something about them i would not recommend using 10 IBOutlets. Instead, i would use the viewWithTag: method, and set each buttons tag accordingly so that you don't have to go to the trouble of creating IBOutlets for each one.
In your case, I would create one IBOutlet for the label, and one IBAction for the buttons. The IBOutlet for the label would be to update the text when the user pressed a button. Your IBAction would look something like this:
-(IBAction)digitPressed:(UIButton *)sender{
//First you have to check if there is currently any text in the label
//If there is not, the below line of code is performed
NSString *textWithDigit = [[yourLabel.text] stringByAppendingString: sender.titleLabel];
//This line updates the label's text
label.text = textWithDigit;
}
I only put in the code relevant to IBActions and IBOutlets (you need an IBOutlet for the label in order to update the text, and you need the IBAction to change the label's text to the digit pressed); there is much more code needed (code needed to check if the user is currently in the middle of typing a number, code for operations like +, -, *, /), but this was just a start.
Hope this helps!

NSTextField / NSSearchField default value that is removed when focus is on the field

I would like to have a default value for a NSTextField (or NSSearchField) and have this default value removed when the user clicks on it and set back when the text view loses the focus and the text field is empty. Like this:
It sounds like I should extend NSSearchField but I was wondering if there was an easier solution.
This functionality is in fact built-in; there's no need to subclass or otherwise extend NSSearchField.
That's the field's placeholder string. You can set it either in IB or via a call to setPlaceholderString:. Notice that this is a method of NSTextFieldCell. An NSTextField is the "public face" of its cell, and has cover methods for almost all of the cell's functionality. In this case, however, you need to send the message directly to the cell:
[[field cell] setPlaceholderString:#"Jumbo jets"];
Since NSSearchField and NSSearchFieldCell inherit from NSTextField and NSTextFieldCell, respectively, the process is the same for them.
Set the place holder string on the NSTextFields's NSTextFieldCell using
[[textField cell] setPlaceholderString:#"String Matching"];