0

I'm new to iOS development and I'm stuck on this one issue...one of many actually.

I have a simple nib with 3 UITextfield and a UILabel. I want to calculate the sum of the 3 numbers entered in each text field without having to use a button action...basically in real-time. enter image description here

@property (nonatomic, strong) IBOutlet UITextField *firstNumber;
@property (nonatomic, strong) IBOutlet UITextField *secondNumber;
@property (nonatomic, strong) IBOutlet UITextField *thirdNumber;
@property (nonatomic, strong) IBOutlet UILabel *total;

I have the implementation set up, but I don't even know how to approach this.

jscs
  • 63,694
  • 13
  • 151
  • 195

3 Answers3

3

First set delegate self to your textfield

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [firstTextField setDelegate:self];
    [secondTextField setDelegate:self];
    [thirdTextField setDelegate:self];
}

Calculate your total in following method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    total.text = [NSString stringWithFormat:@"d",([firstTextField.text intValue])+([secondTextField.text intValue])+([thirdTextField.text intValue])];
    return YES;
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
mandeep-dhiman
  • 2,505
  • 2
  • 21
  • 31
  • 1
    This code works great. However, it requires a return key to calculate. Using the number pad simply didn't work. Is there any way for the fields to auto-calculate a result without the use of a return key? I added a "%" in front of the string @"d" to fix an error. – Maximus Vermillion Dec 16 '11 at 20:33
  • you can add custom toolbar on keyboard with done or next button. – mandeep-dhiman Dec 19 '11 at 10:38
1

There are actually 2 ways to do this.

You can setup a delegate and use:

- (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string;

Or you can use a NSNotificationCenter and use:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];

The delegate function is my favorite, just get the values of all your textfields in this method, add them to eachother and return the value where ever you like.

The notificationcenter pretty much does the same thing but you will have to do the calculating in the selector method (textDidChange:).

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
0

You'll want to limit fields to numbers only. Make sure you have added the class as the delegate if the text fields and :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}

Update the fields on an interval:

- (void)updateTotal
{
     double first = [firstNumber.text doubleValue];
     double second = [secondNumber.text doubleValue];
     double third = [thirdNumber.text doubleValue];

     double tot = first+second+third;

    NSString* totalString = [NSString stringWithFormat:@"%d", tot];
    total.text = totalString;
}
Community
  • 1
  • 1
Nick
  • 1,194
  • 1
  • 10
  • 18
  • When I put this code in, I get an error that says "Receiver type 'UITextField' for instance message does not declare a method with selector 'doubleValue'. next to each of the 3 lines. – Maximus Vermillion Dec 16 '11 at 09:39
  • I made an edit... You need to access the text property of the field... Sorry about that... – Nick Dec 16 '11 at 18:28