I have a segmented control and when the user selects the first segment I want it to pass the number 0, second segment 1, and third segment 2. So that I later can do like this:
NSString *submitFormJS = [NSString
stringWithFormat:#"document.getElementById('usertype').value='**%#**';", **segment**];
where the segment is equal to the number: 0, 1 or 2.
UISegmentedControl has a property named selectedSegmentIndex that you might find useful for this.
Related
I have 9 Segmented Controls, each with 3 Segments.
I have selected Segment 3 on all of them, and now want to disable the first 2 segments on each of them.
Code so far is as below, what changes do I need to make?
[q2SegControl, q3SegControl, q4SegControl, q5SegControl, q6SegControl, q7SegControl, q8SegControl, q9SegControl, q10SegControl].forEach {
segmentedControl.setEnabled=false,forSegmentAt:0,1}
I believe you have to use it like this:
[q2SegControl, q3SegControl, q4SegControl, q5SegControl, q6SegControl, q7SegControl, q8SegControl, q9SegControl, q10SegControl].forEach { segmentedControl in
segmentedControl.setEnabled(false, forSegmentAt: 0)
segmentedControl.setEnabled(false, forSegmentAt: 1)
}
I am relatively new to xCode and I have 2 integer text labels Text1 and Text2 text.
I'm looking for some code that would compare if Text1.text is greater than Text2.text and then if another text field Text3.text would be equal to the value of Text1.text.
Appreciate any help.
In the attributes inspector I assume. Select the text, go to attributes inspector (top right corner at the utility area, forth in a row. Looks like a square head with ears) and check the size.
Well, if you want xcode to check the size, you'll need to make an if or switch statement. Use enums like .fontSize
The text fields are just views that display a string.
You can access the string that they contain via the text property:
NSString *text1 = _text1.text;
Now you have the string that is displayed in the textview ( "34" for example)
You can use the NSString method intValue to turn this string into an integer:
int text1Value = [text1 intValue];
You can now compare the value of this to another integer:
if (text1Value > 42) {
_textLargest.text = [NSString stringWithFormat:"%d", textValue1];
}
Main points:
Text fields are just views; you need to work with the data they are displaying rather than treat them as objects
You need to use the correct type for your comparison. The text field contains text but you need to convert it to a number to make it meaningful
You need to convert the number back into a string if you want it to appear in the text field's text property [NSString stringWithFormat:"%d", textValue1]
You can take and compare the int values as follows
if([Text1.text intValue] > [Text2.text intValue] )
{
Text3.text=[NSString stringWithFormat:"%#", Text1.text];
}
when I update segmented control text, the interface (segment's width) changed and cut some letters.
[segmentedcontoll setTitle:#"test" forSegmentAtIndex:1];
segmentedcontoll.apportionsSegmentWidthsByContent = YES;
How can I solve this ?
EDIT:
It looks like your content has outgrown the dimensions of the standard UISegmentedControl.
If you are okay with smaller font, it's possible to set the entire control to have a smaller font point size, seen here.
Another option is to configure the segments the other supported way.. With images. It's a little bit of a hack, but you can create images on the fly with the UIView Snapshotting API of views/labels configured however you want and set images for each segment instead of using text. This would allow you to create 2 line labels with fixed widths and set images for each section to be images generated from the label as the content changes. More work, but you would still be using the standard class.
The last option, which might work the best for you, is to create some other custom control that does what you would like. After all, UISegmentedControl really is just a nice button container. And it does somewhat seem like you are using the control in a non-standard way - both as a control and an input form section.
Others have gone this route before and created alternatives that you can use.
You can create a separate class as below,
class CustomSegmentedControl: UISegmentedControl {
//code for creating multi line
override func didMoveToSuperview()
{
for segment in subviews
{
for subview in segment.subviews
{
if let segmentLabel = subview as? UILabel
{
segmentLabel.numberOfLines = 0 //just change here the number of lines and check it.
}
}
}
}
}
and create an outlet in your viewcontroller as,
// Initialize
let items = ["Purple", "Green", "New Segment"]
let customSC = CustomSegmentedControl(items: items)
use customSC and do what ever you want to do, similar to segmentedControl object.
I have four segments in my UISegmentedControl. I am trying to programmatically set the selected state of the third and fourth segments at the same time if the user has selected the first segment.
Example: In a given segmented control, if the user selected segment A, then C, D should be selected.
I looked into Apple's methods but found none that match my requirement. I am looking for a method that would look like-
Any help on this is much appreciated.
-(void) setSelected: (BOOL) selected forSegmentAtIndex: (NSUInteger) segment
Parameters
enabled
YES to select the specified segment or NO to unselect the segment. By default, segments are unselected.
segment
An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.
Swift 4.0
segmentedControl.selectedSegmentIndex = index
Use setSelectedSegmentIndex like so -
[mySegmentControl setSelectedSegmentIndex:index]
This code is for swift 2.0
// Declare outlet for our segmented control
#IBOutlet weak var segmentcontroll: UISegmentedControl!
// Declare our IBAction for the segmented control
#IBAction func segmentneeded(sender: AnyObject) {
// Here we test for the presently selected segment
switch segmentcontroll.selectedSegmentIndex {
case 0:
self.view.backgroundColor=UIColor.purpleColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
case 1:
self.view.backgroundColor=UIColor.yellowColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
default:
self.view.backgroundColor=UIColor.grayColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
}
}
The UISegmentedControl works like a set of radio buttons. That is, only one segment can be selected at a time. To get the functionality that you want, you will need to make a custom control that looks like a UISegmentedControl.
First, create an outlet from the segmented control you've added from the UI
#IBOutlet weak var segmentedControl: UISegmentedControl!
Using the code below programmatically change the selected segment
segmentedControl.selectedSegmentIndex = 1 // index
You can simply use selectedSegmentIndex property as there is int he documentation
https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instp/UISegmentedControl/selectedSegmentIndex
So simply:
[segmentControl setSelectedSegmentIndex:2];
Consider that you can set just 1 index and that if you want unselect all you have to set the index -1.
Programmatically this can not be done in SegmentControl but you can do this way:
You can create one imageView put four button on that which size are equal like segments and make them transparent when on one button is pressed change the image of imageView which look like Segment C, D are pressed this way you can do other task.
I have a UITextView that has a fixed width and height. I pre-populate the entire textfield with blanks.
I would like to insert a character with the push of a button that will erase the last blank character, insert my string character and then place the cursor at the beginning of the newly inserted string. I am trying to achieve inserting special fonts right to left and bottom to top.
It is working with the first button push and on the second button push the new value is inserted in the correct position to the left, however, the cursor will not move to the left after the second button push, it remains to the right after the second string insert.
Here is my code...
-(IBAction)chartP:(id)sender {
NSRange currentRange = myChart.selectedRange;
if (currentRange.length == 0) {
currentRange.location--;
currentRange.length++;
}
myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange
withString:[NSString string]];
currentRange.length = 0;
myChart.selectedRange = currentRange;
myChart.text = [myChart.text stringByAppendingString:#"p"];
myChart.selectedRange = NSMakeRange(myChart.selectedRange.location -1, 0);
}
Can someone assist me with what I am missing here to continually increment to the left with my string inserts?
How about flipping the text area:
myChart.transform = CGAffineTransformMakeScale(-1,-1);
It sounds like you are trying to implement right-to-left text direction by faking it. Why not do the real thing?
Here's a question that covers the topic:
Change the UITextView Text Direction
If you need bottom-to-top entry, and you have the ability to use a custom font, perhaps you can apply a transformation to the UITextView and y-flip it. Look at the transform property of UIView. (Things like the text selection loupe may break, but it's worth a try.)