Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this questionI have two UITextFields
one field is RISK and other is WIN. Now when user start writing in RISK field then it should accordingly calculate the value of WIN and should be filled in WIN field. Formula is this
if (a<0) {
WIN = RISK/(-a/100)
}
else {
WIN = RISK*(a/100)
}
and if value is entered in WIN then it should populate the value in RISK, formula is if (a<0) {
RISK = WIN*(-a/100)
}
else {
RISK= WIN*(100/a)
}
so please guys guide me how can i populate the fields without pressing any button and 开发者_运维知识库how can i calculate the values? Thanx in advance
You should implement the UITextFieldDelegate. Set a class, perhaps your view controller to the delegate of the WIN textfield, then implement this method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Inside the method, convert the string to a number, run your formula, change the value of the RISK textfield and return YES.
Assuming you have a property for each field:
@property (nonatomic, retain) IBOutlet UITextField win;
@property (nonatomic, retain) IBOutlet UITextField risk;
and IBAction
methods:
- (IBAction) winDidChange: (id) sender;
- (IBAction) riskDidChange: (id) sender;
Hook these up in interface builder to the Editing Changed event:
Your method will look something like:
- (IBAction) winDidChange: (id) sender
{
float winValue = [self.win.text floatValue];
float riskValue = <your calculation here>
self.risk.text = [NSString stringWithFormat: @"%f", riskValue];
}
.. and similarly for riskDidChange:
If you hook up the Editing Changed event of the win
field to the winDidChange:
IBAction
in interface builder (and the same for risk
), the method will be called every time a character is typed into the field.
You should read The Target-Action Mechanism section in http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html
You can implement the delegate method (UITextFieldDelegate) to make use of the following methods....
- textFieldShouldBeginEditing:
- textFieldDidBeginEditing:
- textFieldShouldEndEditing:
- textfieldDidEndEditing:
- textField:shouldChangeCharactersInRange:replacementString:
When a value changes, you can use these delegate methods to update your other value.
all you need to do is
[myTextField addTarget:self action:@selector(myFunction:) forControlEvents:UIControlEventEditingChanged];
and then
- (void)myFunction:(id)sender {
//calculate and show in WIN field
}
精彩评论