Vì vậy, tôi đã thêm các mục tiêu vào IBActions của tôi mà tôi đã tạo xảy ra khi giá trị của trường văn bản thay đổi. Khi các hành động này xảy ra, hệ thống sẽ kiểm tra xem hai trường văn bản có phải là cả hai số nguyên hay không. Tôi đã đặt hai biến được đặt thành false và chúng được đặt thành true khi cả hai biến là int. Trong IBActions, tôi có các câu lệnh cho biết một nút được kích hoạt nếu cả hai biến chứa số nguyên. Khi tôi chạy trình mô phỏng, nút này không kích hoạt khi cả hai trường văn bản chứa một số nguyên.Swift: Sự kiện kiểm soát thay đổi giá trị không gọi?
Tôi mới đến nhanh chóng, vì vậy nếu có thể, vui lòng viết tất cả mã và vị trí cần có trong mã của tôi. Dưới đây là những gì tôi có cho đến nay:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var calculatorButton: UIButton!
@IBOutlet weak var inspirationLabel: UILabel!
@IBOutlet weak var beginningLabel: UILabel!
@IBOutlet weak var calculatorContainer: UIView!
@IBOutlet weak var answer1Label: UILabel!
@IBOutlet weak var doneButton: UIButton!
@IBOutlet weak var yourWeightTextField: UITextField!
@IBOutlet weak var calorieNumberTextField: UITextField!
@IBOutlet weak var menuExampleButton: UIButton!
@IBOutlet weak var aboutButton: UIButton!
@IBOutlet weak var calculateButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
yourWeightTextField.delegate = self
calorieNumberTextField.delegate = self
calculateButton.enabled = false
// Calling the textfield valueChanged Methods
yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func calculatorButtonTapped(sender: AnyObject) {
calculatorContainer.hidden = false
inspirationLabel.hidden = true
beginningLabel.hidden = true
menuExampleButton.hidden = true
aboutButton.hidden = true
}
var yourWeightFilled = false
var calorieNumberFilled = false
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Find out what the text field will be after adding the current edit
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
// If the textfields have the properties of the function
if textField == yourWeightTextField {
yourWeightFilled = text.toInt() != nil
} else if textField == calorieNumberTextField {
calorieNumberFilled = text.toInt() != nil
}
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder();
return true;
}
// The methods to close the keyboard when editing is finished
@IBAction func yourWeightEditingDidEnd(sender: AnyObject) {
yourWeightTextField.resignFirstResponder()
}
@IBAction func calorieNumberEditingDidEnd(sender: AnyObject) {
calorieNumberTextField.resignFirstResponder()
}
@IBAction func yourWeightValueChanged(sender: AnyObject) {
// If both variables are true and the text fields contain integers, enable button
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
@IBAction func calorieNumberValueChanged(sender: AnyObject) {
// If both variables are true and the text fields contain integers, enable button
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
}
}
Bạn đã quên 'n' trên 'EditingChanged' trong mẫu mã thứ hai. – adheus
@adheus - cố định, cảm ơn. – Piotr
@Piotr nhưng nó chỉ hoạt động khi tôi đang chỉnh sửa textfield với bàn phím. Tôi nên làm gì khi cần thay đổi textfield bằng một nút bấm? – MHSFisher