फ़ंक्शन का उपयोग करने के नीचे आप पाठ क्षेत्र आप गणना करना चाहते हैं की UITextFieldDelegate प्रोटोकॉल को लागू करने की जरूरत है, इस समारोह हर बुलाया जाता UITextField परिवर्तन मूल्य:
आपका वर्ग घोषणा इस
कुछ ऐसा दिखाई देगा
class ViewController: UIViewController, UITextFieldDelegate
आप इस
@IBOutlet var txtValue: UITextField
के लिए एक
@IBOutlet
जैसा होना चाहिए 210
viewDidLoad में आप स्वयं
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
//change the value of the label
mylabel.text = String(newLength)
//you can save this value to a global var
//myCounter = newLength
//return true to allow the change, if you want to limit the number of characters in the text field use something like
return newLength <= 25 // To just allow up to 25 characters
return true
}
के लिए प्रतिनिधि विधि सेट याद रखें इस समारोह सभी UITextField के लिए कहा जाता है, इसलिए यदि आप कई UITextField है आप एक तर्क जोड़ने के लिए डायन भी इस समारोह बुला रहा है जानने की आवश्यकता होगी, बस जोड़ने प्रत्येक UITextField लिए कोई अलग टैग और एक स्विच बयान समारोह
इसे लेबल से कैसे कनेक्ट करें? क्षमा करें मैं शुरुआती हूं – Xrait
ठीक है, मुझे यह मिला, लेकिन मेरे टेक्स्ट फ़ील्ड के नीचे छोटे लेबल को कैसे बदला जाए? – Xrait
क्षमा करें, मुझे नहीं पता कि आप किस लेबल का जिक्र कर रहे हैं। – Icaro