C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example UITextView: Swift
//
// ViewController.swift
// ExampleTimeH
//
// ...
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var simpleTextView: UITextView!
@IBOutlet weak var simpleButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func actionTriggered(sender: AnyObject) {
}
}
Count: We count the characters entered in the UITextView and store this in the "count" local.
SetTitle: We invoke the setTitle() method on the UIButton outlet. We set the text to the count of characters in the text view.
Example UIButton, UITextView: Swift
//
// ViewController.swift
// ExampleTimeH
//
// ...
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var simpleTextView: UITextView!
@IBOutlet weak var simpleButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func actionTriggered(sender: AnyObject) {
// The button was clicked.
// Get characters count from UITextView.
let count = simpleTextView.text.characters.count
// Change title of the UIButton.
simpleButton.setTitle("Chars \(count)", forState: UIControlState.Normal)
}
}
ACB-def: I used the first 6 letters of the alphabet with a hyphen in between and then clicked the button. The button now reads "Chars: 7."