I am reading the free book from Apple Education titled "Develop In Swift Fundamentals". I am on the chapter titled "Controls In Action" and the section of the chapter I'm stuck on is titled "Programmatic Actions" (page 287).
The section has me connect a UIButton programmatically by first manually typing in
@IBOutlet var button: UIButton!
then connecting the button using this code below.
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
I pasted the code above right after the viewDidLoad function, however I get a fatal error.
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I've attached a link to the Github repository if you want to download the code and run it in Xcode.
[https://github.com/campusgateapp/CommonInputControls)
import UIKit
class ViewController: UIViewController {
@IBOutlet var toggle: UISwitch!
@IBOutlet var slider: UISlider!
@IBOutlet var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}
@IBAction func buttonTapped(_ sender: Any) {
print("Button was tapped!")
if toggle.isOn {
print("The switch is on!")
} else {
print("The switch is off.")
}
print("The slider is set to \(slider.value).")
}
@IBAction func switchToggled(_ sender: UISwitch) {
if sender.isOn {
print("Switch is on!")
} else {
print("Switch is off!")
}
}
@IBAction func sliderValueChanged(_ sender: UISlider) {
print(sender.value)
}
@IBAction func keyboardReturnKeyTapped(_ sender: UITextField) {
if let text = sender.text {
print(text)
}
}
@IBAction func textChanged(_ sender: UITextField) {
if let text = sender.text {
print(text)
}
}
@IBAction func respondToTapGesture(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: view)
print(location)
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Created
I have a word document on my MacBook, I airdropped it to my iPhone but Microsoft Outlook keeps opening it as an attachment in a new email. I want to save the file to my phone, not open in Outlook. How do I change this?
In Xcode, there is a keyboard shortcut to hide the navigator, ⌘,0. But it stopped working. I went to system preferences and manually added it back a few days ago but it disappeared again today suddenly. Why does this keep happening?