I am a novice in Programming and have just started Objective C. I have created a text field in which I want to allow a “.” to be entered only once! From what I understand, rangeOfString should be able to do this.
-(IBAction)floatingPoint:(UIButton *) sender {
NSString *floatingPoint = [sender currentTitle];
NSRange range = [floatingPoint rangeOfString:@"."];
if (range.location == NSNotFound) {
self.display.text =
[self.display.text stringByAppendingString:floatingPoint];
}
}
From what I understand the code floatingPoint rangeOfString:@”.” would check for “.” in currentTitle and if it is not found should append this string with a “.”
But this is not happening, no decimal point is being entered. If I enter only self.display.text = [self.display.text stringByAppendingString:floatingPoint];
then the text field allows more than 1 decimal points to be entered.
Sorry if I described the problem incorrectly, I am new to all this stuff and this has just begun to sink in.
You’re checking the button’s title for a
.. You probably set the button’s title to.in your nib, so of course it always contains a.!You need to check your text field’s text for a
.instead. Try this: