when i scroll the UISlider, i want to display the current value in UITextField, however i see the following string nan instead of the numeric value of the slider, can you help me please :
- (IBAction)sliderValueChanged:(id)sender
{
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [sender value]];
}
the connections in IB are correct, also, the min value for the slider is 0.0 and the max one is 70, i need to display the current position (i.e : 20) in the UItextField
Cast the sender to UISlider:
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [(UISlider *)sender value]];
Related
I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!
You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.
-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
First You Can Set static int count = 0;
I have a uislider connected to a label, when the slider moves, the label adjusts its value. I am able to set the increments and the max and min, but i can't figure out how to remove the decimal places from the value..i simply want whole steps. suggestions please
- (IBAction)sliderValueChanged:(id)sender {
self.myLabel.text = [NSString stringWithFormat:#"%f", (roundf(self.mySlider.value)];
}
Display the result as an int instead of a float.
self.myLabel.text = [NSString stringWithFormat:#"%d", (int)roundf(self.mySlider.value)];
I want to add a string in the highlighted area in the textview, I mean by the highlighted area, where the blue line is located.
So once the user click on the button it adds a string where the "blue line" is located
I used stringByAppendingString but it adds the string after the word exists only
NSRange range = myTextView.selectedRange;
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];
myTextView.scrollEnabled = NO; // turn off scrolling
NSString * insertingString = [NSString stringWithFormat:#"your string value here"];
myTextView.text = [NSString stringWithFormat: #"%#%#%#",
firstHalfString,
insertingString,
secondHalfString];
range.location += [insertingString length];
myTextView.selectedRange = range;
myTextView.scrollEnabled = YES;
You need to use the selectedRange to find out where the text cursor is. Then use replaceCharactersInRange:withString: or insertString:atIndex: to insert the new text into the original text. Then update the text into the view.
Even though its not clear what you are trying to achieve, it seems that you want the user to start editing the textfield from the position where text starts. In that case , you can refer following:
Hint 1
Set your view controller (or some other appropriate object) as the text field's delegate and implement the textFieldDidBeginEditing: method like this:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITextPosition *beginning = [textField beginningOfDocument];
[textField setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
}
Note that setSelectedTextRange: is a protocol method of UITextInput (which UITextField implements), so you won't find it directly in the UITextField documentation.
Hint 2
self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];
Hint 3
finding-the-cursor-position-in-a-uitextfield/
I am new in programming, let's say I am still swimming in the ocean. I am trying to set up an UIStepper for change a value on a text field. This value has to change using the decimal number (step value = 0.1), I have read many documents but still not sure from where need to start.
The following is the code I am using:
- (IBAction)valueChanged:(UIStepper *)sender {
sender.stepValue = 0.1;
float value = [sender value];
self.txtRspeed.text = [NSString stringWithFormat:#"%.0f", value];
seems like I get the step Value that I want 0.1 but doesn't show up in the simulator. I also tried setting as initial value the one in the text field with some code but I still encounter errors.
What I am doing wrong?
- (IBAction)valueChanged:(UIStepper *)sender {
_value = txtRspeed.text.floatValue;
sender.value = self.stepper.stepValue + _value;
self.txtRspeed.text = [NSString stringWithFormat:#"%g", sender.value];
}
(void)viewDidLoad{
[super viewDidLoad];
self.stepper.value = 0.1f;
self.stepper.minimumValue = -100.0f;
self.stepper.maximumValue = 100.0f;
self.stepper.stepValue = 0.1f;
}
I am getting the increment but no the decrement.
If a set the stepper value in a viewDidLoad it start from 0
So create the UIStepper and set its stepValue property to 0.1. You can get the current value of the control using it's value property.
You'll probably want to create the stepper in your storyboard or .xib file and connect it to an IBOutlet in a view controller so that your view controller can access it easily.
I have a UISearchBar on which I am trying to set a cursor position. I am using UITectField delegates as I couldn't find anything direct for UISearchBar. Below is the code I am using:
UITextField *textField = [searchBar valueForKey: #"_searchField"];
// Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:selectedRange.start toPosition:selectedRange.end];
Question is in the 'newRange' object for 'toPosition' I want to have something like selectedRange.end-1; as I want the cursor to be on second last position.
How do I set the cursor to second last position?
Swift 5
I came across this question originally because I was wondering how to convert UITextPosition to an Int (based on your title). So that is what I will answer here.
You can get an Int for the current text position like this:
if let selectedRange = textField.selectedTextRange {
// cursorPosition is an Int
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
}
Note: The properties and functions used above are available on types that implement the UITextInput protocol so if textView was a UITextView object, you could replace the instances of textField with textView and it would work similarly.
For setting the cursor position and other related tasks, see my fuller answer.
the clue is to make a position and then a range with no length and then select it
e.g.
- (IBAction)select:(id)sender {
//get position: go from end 1 to the left
UITextPosition *pos = [_textField positionFromPosition:_textField.endOfDocument
inDirection:UITextLayoutDirectionLeft
offset:1];
//make a 0 length range at position
UITextRange *newRange = [_textField textRangeFromPosition:pos
toPosition:pos];
//select it to move cursor
_textField.selectedTextRange = newRange;
}