我正在處理用戶有5個文本字段來輸入數據的項目。 「計算」按鈕根據用戶輸入計算所需的計算。 我的解決方案是創建5個IBOutlet UITextField並將它們連接到一個xib文件中。每個文本字段都使用以下代碼處理基於輸入的事件。有人能告訴我一種解決問題的有效方法嗎?有人能告訴我一個更有效的方式來處理多個文本字段代表?
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if(textField == self.numberOfNightsTF){
self.numberOfNights = [self.numberOfNightsTF.text intValue];
NSLog(@"The number of nights is set to %.d days", self.numberOfNights);
}
else if(textField == self.nightlyChargeTF){
self.nightChargeCost = [self.nightlyChargeTF.text floatValue];
NSLog(@"The cost of charge per night is set to %.2f$", self.nightChargeCost);
}
else if(textField == self.roomServiceTF){
self.roomServiceCost = [self.roomServiceTF.text floatValue];
NSLog(@"The additional room service cost is set to %.2f$", self.roomServiceCost);
}
else if(textField == self.telephoneTF){
self.telephoneCost = [self.telephoneTF.text floatValue];
NSLog(@"The additional telephone service cost for telephone is set to %.2f$", self.telephoneCost);
}
else if(textField == self.miscCostTF){
self.miscCost = [self.miscCostTF.text floatValue];
NSLog(@"The miscellaneous is now set to %.2f$", self.miscCost);
}
[textField resignFirstResponder];
return YES;
}
標題可以重寫,但這個問題顯示足夠的研究和代碼,以保持開放。 –