I have multiple sliders in my application,how to know which slider is selected. And how to set the slider values to integer type where double is default type;
You can use the Tag property of the slider to assign an integer (or even better, use an enum). This is useful if you have multiple buttons or controls with the same target.
The sender will be the control that triggered the action, and you can get the tag from that. You can also cast back to the original control type if you need to access other properties.
See Objective C IBOutlets for information on the sender.
Also note that normally you would set the tag in Interface Builder (IB), but you can also set them in code.
You should set up the slider to target a method when it changes, that way you can be informed when a slider changes. You should do this in interface builder but if for some reason you can't, perhaps you have to dynamically determine the sliders needed then you can use methods like
[NSControl setAction:]
[NSControl setTarget:]
NSControl can have their value set with various data types (int, float, double) even some types that are not applicable to NSSliders (for example NSString), their is no default type, just use the following method.
-[NSControl setIntValue:]
Related
I'm trying to set up a text view that a user can type Hebrew text into from right-to-left. Currently it defaults to a left-to-right text direction, which the user can manually change by right-clicking and selecting "Writing Direction > Right to Left", but what I need is for the text view to always default to this, without requiring the user to set it manually.
There's an option for setting this in Interface Builder, which is always ignored when I build and run my app.
I would be fine setting it from code, but I can't figure out how to use the method that looks like the closest thing to what I need:
hebrewTextView.setBaseWritingDirection(NSWritingDirection.RightToLeft, range: <#NSRange#>)
The range is stumping me. If it takes a range can this be the method I need? Wouldn't the default behavior of a field be independent of any range of text?
Is there a way to set the default writing direction for a NSTextView? Can this be done from the storyboard/interface builder, or from code? If setBaseWritingDirection is the method for this, what is the range value for and how would I set it for a field that is initially empty?
NSTextView inherits from NSText. NSText has a baseWritingDirection property. Try setting that:
hebrewTextView.baseWritingDirection = NSWritingDirection.RightToLeft
It also inherits the action method makeBaseWritingDirectionRightToLeft() from NSResponder, which presumably what the contextual menu uses. So, you could call that.
If neither of those works, you can set the text view's defaultParagraphStyle to one whose baseWritingDirection is right-to-left:
var style = hebrewTextView.defaultParagraphStyle.mutableCopy() as! NSMutableParagraphStyle
style.baseWritingDirection = NSWritingDirection.RightToLeft
hebrewTextView.defaultParagraphStyle = style
I’m working on a little optical-illusion app. As part of that, I have a model key (a CGFloat) representing an angle.
I have three controls — an NSTextField, an NSStepper, and an NSSlider — each bound to that model key. (The NSTextField was created as a “Text Field with Number Formatter”.)
I want that angle to fall between -45 and 45 degrees at all times. I also want it rounded to the nearest integer.
To that end, I’ve implemented a setAngle method that applies those rules. In addition to rounding its input, it replaces any value falling outside the acceptable range with the closest valid value.
I notice that whenever I use one of the controls to change the angle’s value, the other two controls reflect the post-processing value — but the submitter itself does not.
For instance, if I move the slider, the text field shows the rounded value, not the possibly-fractional value that the slider submitted.
Likewise, if I enter 44.5 in the text field, the slider’s position corresponds to 45.
However, the submitting control still displays the “raw” value it submitted: the text field in the last example continues to read 44.5.
Placing
[self willChangeValueForKey:#"angle"];
and
[self didChangeValueForKey:#"angle"];
around the code in setAngle that actually changes the value had no effect on this.
My principal questions, then, are these:
Is there, generally speaking, a “correct” way to alter a value submitted by a control, such that all controls bound to the key get the updated value? I'm not entirely sure that angleSet is the right place to pull the sort of post-processing shenanigans I am, but I'm even less sure where else I should do so. (The Apple docs regarding validation in key-value coding expressly discourage using validation to this end.)
If there's no general-purpose mechanism for setting a tweaked version of model key, and notifying all associated controls after the fact, is there a way to identify the single control doing the actual setting, and update it with the post-processed value?
Thanks in advance!
use cocoa binding is the most easiest way for this situation.
declare a property
#property CGFloat angle;
and bind it to value of the controls with keypath angle or someobject.angle (it depends on your implementation)
I am building a UI in Interface Builder and am looking for the simplest (least code) way to identify an element from code.
I'd like to avoid using outlets because frankly I detest visual programming and don't want to pollute my class space with countless outlet properties. Is there some unique string identifier I can assign to static elements that I can either reference directly or easily look up from code?
Ideally I just want to look up an object by its id like I can do in JavaScript:
document.getElementById('myIdentifier');
I agree with rightfold that outlets are the best solution, but there is an answer that addresses your question directly: you can use the (integer) tag property of UIView (setting it either in IB or in code), and then you can fetch the view with the method UIView -viewWithTag:.
Successive calls to -viewWithTag: will iterate through the subviews that have the given tag. Because it's an integer, you'll probably want to use named constants for tag references in code, but unfortunately there's no way (that I am aware of) to use constants in that manner in IB.
The default value for the tag property is 0, so avoid using that as a semantically meaningful tag.
I would think you should be able to define a label on a display method. However, I can't see it in the properties window. But there is a Best Practice Warning:
Control label is a copy of its display method label
So who can either tell me how to add the label or what this error message is really about?
The return type for the display method should be an Extended Data Type. On the EDT you should supply the label.
Each form control must have a label either inherited from EDT/Enum or explicitely set on the control. The label set on the control must not be the same as the EDT/Enum's label. I suppose in your scenario it is the same, and in order to get rid of this BP warning you should just clear the label in the control properties. If you want the label to be different you can change it in the control properties or in the Extended Data Type/Base Enum properties but then it will be changed everywhere througout the system.
Let's say I have a Size class which has height and width properties (in reality the class is a bit more complex than this, but Size makes a good example).
I want to display this as $width x $height in my UI.
The obvious way to do this is to bind to a dimensions property which is dependent on width and height.
My question is where is the best place to add this new property?
I could add it to the Size class itself in the modal, but then if another controller wants to display the string different I'm stuck creating yet another property. I'm also a bit reluctant to do this because in my case the Size class is in a framework that will be used in a couple different places (although likely all from code I have control over).
I could add it in a category to the Size class inside the project containing the view/controller so it will only be availiable in the places I know it will be used, but in various places I've seen suggestions that categories tend to be overused, and am forced to wonder if this is one of those cases.
In the case of a single Size instance I could create the property in the controller class containing it, but this becomes more difficult when you have an array of Sizes.
I could bind to the entire size object and use a transformer to turn them into strings, but binding to an array of sizes would then cause you to have to transform each element of the array into a new array in the transformer, which seems a bit ugly.
If want to display this composite value as a string, then bind "Display Pattern 1" of a text field to the width property and "Display Pattern 2" (shown when you bind Display Pattern 1) to the height property. In the Display Pattern 1 binding, set the "Display Pattern" to %{value1}# x %{value2}# (yes, slightly unintuitive syntax). This will give a text field that displays "[width] x [height]" and will update on changes in either property.
You can do the same with an NSTextFieldCell, e.g. as the cell in an NSTableColumn. The downside of this method is that the text field will not be able to edit the bound width and height values. You would have to write an NSValueTransformer if you need to be able to edit them.
Using multiple bindings through a display pattern as Barry suggested sounds like the best approach, at least without knowing more about your UI. I've used the same thing in the past, where I built an inspector for an array of images and had the dimensions bound to a single text field.
In general it's good practice to use value transformers or formatters if you can, but in cases of last resort there's nothing wrong with using a category. I've done this in the past when I had trouble binding to a date, but breaking it down into individual time and date pieces. A category is a good approach because it lets you maintain separation with the model, but you don't need to do anything crazy like binding directly to the controller.
I want to display this as $width x $height in my UI.
Why not two fields? Then you could make them editable.
(in reality the class is a bit more complex than this, but Size makes a good example)
Assuming the above is not feasible in your real situation, you might try creating a custom subclass of NSFormatter, and setting it as the formatter on the cell (I assume this is in a table view, since you wouldn't bind a single control to an array). You would then bind to whole Size objects.