automatically updating a label objective-c without clicking a button - objective-c

I have 4 labels, 3 are editable with one label that cant be edited and one float which is the sum of the 3 editable labels. Whats the easiest way to get the uneditable label to update automatically with the summed value of the other labels as the user edits them? (i dont want the user to have to click a button after the user has edited the labels) I know i'll probably be using bindings but your help is very appreciated.

Assuming your three editable labels are actually "text fields", designate some object (probably your view controller) as a delegate and then respond to this NSControlTextEditingDelegate protocol method:
- control:shouldEndTextEditing:
When the user tabs or exits the editable text field, you'll catch that event via the protocol method and then you can update that non-editable text field (which you've set to an outlet in your view controller, right?).
Or, you can use bindings, yes. :-)

Related

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).

How can I make command-A select all the NSTextView text in rows in an NSTableView?

So if I have an NSView based tableview and inside the views are NSTextViews which are non-editable but selectable...
how can I get that nice functionality of command-A selects all the text? I don't mean row selection. I have row selection disabled for the tableview. I mean highlighting the text in blue so you can copy it to your clipboard. But not just 1 NSTextView's text from one row, all of them from all the rows.
And in addition to command-A click and drag should do this too. But out of the box it seems I can only select one row's text. Here is video showing problem:
https://dl.dropboxusercontent.com/u/2510380/table.mov
(i keep clicking and dragging but can't highlight text on the next row)
here are two mac apps (skype and gabble) that do this:
https://dl.dropboxusercontent.com/u/2510380/skype.mov
and
https://dl.dropboxusercontent.com/u/2510380/gabble.mov
Assuming they are NOT using WebViews with just HTML inside, how do you get this control over the clipboard? i.e. in Skype you select the text and only the conversation is highlighted, not the timestamp of each message. Also the text copied to the clipboard is formatted very nicely. Can you point me in the right direction to reverse engineer skype?
Unfortunately there's no way to do this easily. This is because only ONE control can be the first responder at a time. This means that, though you can have selection in multiple text views, there are several problems:
Only one text view's text will actually be highlighted with the "live" highlight color; the others will have the gray highlight of non-focused controls.
Copy commands will only apply to the first responder text view.
Drag session starts will be initiated from the control the mouse was actually pointing at (irrespective of first responder) and will only drag that control's text.
In a view-based table view, the controls may not even "exist" for a row not currently displayed, so it'll never get the message unless you forcibly create each row, which could be costly for a large table.
Knowing all this, you might be able to "fake it" by having your controller be complicit in a text view and table view subclass's special handling of a select-all message when it's first responder. On receiving this message, the text view subclass can call super then notify the controller (to get its default behavior AND to let you know it happened), at which point the controller can turn around and send the command to all (existing) text views. Highlighting can be spoofed by overriding the text view's drawing and a drag initiation could defer to a delegate (the controller), which would handle writing ALL the strings from your model to the pasteboard (not even touching the text views in possibly-nonexistent row views). The table view subclass would simply pass the same select-all message to the controller without calling super (and even forcibly making sure nothing is selected before returning for good measure).
I hope this helps. If I've forgotten any of your requirements, let me know.
Try like this:-
First create button programatically then write this code after you create button and also write this code in your load method or awakefromnib method.
NSButton *Buttn=// alloc initwithframe;
[Buttn setKeyEquivalentModifierMask:
NSCommandKeyMask];
[Buttn setKeyEquivalent:#"A"];
[Buttn
setAction:#selector(yourmeth:)];
[Buttn setTarget:self];
// now when you press cmd a write
below code in action method
- (void)selectRowIndexes:(NSIndexSet
*)indexes byExtendingSelection:
(BOOL)extend

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.

Modify multiple NSTextView with commands

I'm wondering if anyone knows if it is possible to modify multiple NSTextViews with a menu bar command. For example if the user selects "Bold" from the menu bar, the different NSTextViews that are selected update all their content to show bold.
Here is the set up I have:
#interface MyCustomTextField : NSView <NSTextViewDelegate>{
NSTextView *textView;
BOOL selected;
...
}
So basically I have my own custom class and within each custom class I have a NSTextView, a var determining whether or not this view is selected and some other stuff.
I'm able to select multiple fields however from what I've read on Apple documentation every NSTextView in the window shares one field editor. When a user edits a NSTextView they are actually sending commands to the field editor which processes it and routes it to NSTextView. If this is the case does that mean that I need to create my own custom field editor and route the commands to all my custom selected text classes instead?
==edit==
My CustomTextField classes have a variable named "selected" (see above) and by holding the shift or apple key down I'm able to "select" multiple CustomTextField instances (I put a mask in front of the NSTextView instances which catches the mouseDown message).
So by this selection, multiple instances have their "selected" attribute set to true. As far as the first responder for the window, it would be set to the mask that shows the blue halo around all the NSTextViews.
I'm wondering if I can tell the app to accept default NSTextView commands (such as bold, italicize, etc.) and that if I supply a custom field editor, it will pass all the appropriate messages to the selected CustomTextFields which will then pass it on to the NSTextViews.
In my head the message would be passed like this:
User Submits Text Toolbar Command > Custom Field Editor > MyCustomTextField > NSTextView
Hopefully my explanation made sense or maybe I'm in LaLa land now.

Objective-C & Interface Builder for Dummies: How do I bind the keyboard?

So, I've been reading through other questions on here and Xcode's documentation, but I'm still a bit confused. Here's the scenario: I have a login screen for my app which has 3 text fields and 1 button. Each text field is bound to have a Return Key in IB: Next, Next, Go.
Now, how do I bind these return keys to actual actions where field 1 moves to the next, then field 2 moves to the next, and last field 3 triggers the button?
Some answers for similar questions suggest the use of textFieldShouldReturn method, but I'm still fuzzy on it. Somehow I can't see how it auto-magically knows what to do without having some kind of binding...
What you need to do is use an IBAction to select the next field when the return key is pressed. To do this, first add a method like this to your UIViewController. Make sure to add the method declaration to your header.
- (IBAction)selectSecondField:(id)sender
{
//_secondField is the IBOutlet to the second field
[_secondField becomeFirstResponder];
}
Build and then go to Interface Builder. Select the first text field and open the connections inspector (⌘2) and drag from the dot next to "Did End On Exit" to your controller with the action. A menu of actions will pop up, select selectSecondField.
You will need an action for each field. Alternatively, you could have one action and use sender to see which field was returned.
There's a handle (in the connections panel) on each text field called "nextfield" or something along those lines? (I can't remember off the top of my head) But if you drag that to the next box, that's how it knows (manually). I think generally it guesses using the X/Y location, but when you need to manually do it, just drag that handle.