C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Name: I chose the name "simpleToolbar" for my toolbar. This tutorial is going to keep things simple.
Action: We use a Selector string as the action (the fourth argument to UIBarButtonItem).
StringSelector: To target a func with name "startAction" we use the string "startAction:" with an ending colon.
Items: We add the UIBarButtonItem to the "items" collection on the UIToolbar instance.
Example UIToolbar: Swift
//
// ViewController.swift
// ExampleTimeC
//
// ...
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var simpleToolbar: UIToolbar!
@IBOutlet weak var simpleLabel: UILabel!
var number = 0
override func viewDidLoad() {
super.viewDidLoad()
// Create an item to place on our UIToolbar.
// ... Use "Start" as the text.
// ... Use Plain as the style (this is not deprecated).
// ... Use self as the target (this object is targeted).
// ... Use special selector string to reach startAction func.
let item1 = UIBarButtonItem(title: "Start",
style: UIBarButtonItemStyle.Plain,
target: self,
action: "startAction:")
// Add to items collection.
simpleToolbar.items?.append(item1)
}
func startAction(barButtonItem: UIBarButtonItem) {
// This func is targeted by the selector string "startAction:."
// ... The parameter is required.
// Write a message to the label.
simpleLabel.text = "Start action \(number)"
number++
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Note: In the screenshot, we see the "before" and "after" shots. We tap on the Start item.