2014-11-18 13 views
5

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 
    } 
} 
} 

Trả lời

9

Bạn nên tìm kiếm EditingChaged sự kiện, không ValueChanged

EDIT: Những gì tôi có nghĩa là thay đổi từ:

yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged); 
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged); 

tới:

yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.EditingChanged); 
calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.EditingChanged); 

Bạn chỉ cần tìm kiếm đêm trước sai nt.

+0

Bạn đã quên 'n' trên 'EditingChanged' trong mẫu mã thứ hai. – adheus

+0

@adheus - cố định, cảm ơn. – Piotr

+0

@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

2

Nếu bạn đang tìm kiếm một văn bản thay đổi sự kiện, sau đó Right Click trên các lĩnh vực văn bản chọn Editing Đã End từ Sự kiện Sent. Bạn có thể thấy một vòng tròn ở bên phải kết thúc nhấp vào vòng tròn Giữ phím Ctrl và kéo nó vào tệp ViewController của bạn. Đặt tên cho Hành động bạn muốn và Click connect. Tôi đã cung cấp một số ảnh chụp màn hình cho việc này. Ở đây tôi đặt tên cho hành động TextChanged

Tôi đang dùng Xcode 7 Swift 2 đây

Kích chuột phải vào hộp văn bản và Bạn có thể nhìn thấy như thế này enter image description hereenter image description here

Cuối cùng Bạn có thể xem Đã tạo sự kiện TextChanged. khi bạn nhập nội dung nào đó vào hộp văn bản và nhấp vào quay lại sự kiện này sẽ kích hoạt.

Các vấn đề liên quan