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)
}
}
Post
Replies
Boosts
Views
Activity
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?
When the Code Completion popup used to appear it would show the return value of the code to the left kind of greyed out. In Xcode 12.2 it's gone. Where is it?
For instance, if you create a new button and place it on the view, then select the button, 6 little white squares appear around the border. What is the technical term for these?
For example, how can I make MapView, which is a 'struct' type conform to CLLocationManagerDelegate which requires a 'class' type for its protocol?
struct MapView: UIViewRepresentable, CLLocationManagerDelegate {
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ uiView: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 0.0019, longitudeDelta: 0.0019)
let region = MKCoordinateRegion(center: coordinate, span: span)
uiView.setRegion(region, animated: true)
uiView.mapType = .hybridFlyover
}
}
This code below pulls up the map view in my iOS app. I want to show the users location with a blue dot like in google maps.
What code do I insert and where to make this happen?
Note: I am looking through the MapKit documentation section of the Apple Developer Documentation website. There is a section called "Displaying the User's Location". I just can't seem to figure out how to integrate the given codes into my code. Such as var showsUserLocation: Bool { get set }
etc.
Also note, the code I'm using is from an apple developer tutorial https://developer.apple.com/tutorials/swiftui/creating-and-combining-views
section 5 step 4.
import SwiftUI
import MapKit
import UIKit
import CoreLocation
struct MapView: UIViewRepresentable {
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ uiView: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 50)
let region = MKCoordinateRegion(center: coordinate, span: span)
uiView.setRegion(region, animated: true)
uiView.mapType = .hybridFlyover
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView(coordinate: landmarkData[0].locationCoordinate)
}
}
I am working on the developer tutorial.
Drawing and Animation
Drawing Paths and Shapes
https://developer.apple.com/tutorials/swiftui/drawing-paths-and-shapes
Section 2 Step 9
How do you change the shape of the hexagon?
For example, what do I change in the code to add a side, making it a heptagon?
struct BadgeBackground: View {
var body: some View {
GeometryReader { geometry in
Path { path in
var width: CGFloat = min(geometry.size.width, geometry.size.height)
let height = width
let xScale: CGFloat = 0.832
let xOffset = (width * (1.0 - xScale)) / 2.0
width *= xScale
path.move( to: CGPoint(
x: xOffset + width * 0.95,
y: height * (0.20 + HexagonParameters.adjustment)))
HexagonParameters.points.forEach {
path.addLine( to: .init(
x: xOffset + width * $0.useWidth.0 * $0.xFactors.0,
y: height * $0.useHeight.0 * $0.yFactors.0 ) )
path.addQuadCurve( to: .init(
x: xOffset + width * $0.useWidth.1 * $0.xFactors.1,
y: height * $0.useHeight.1 * $0.yFactors.1
), control: .init(
x: xOffset + width * $0.useWidth.2 * $0.xFactors.2,
y: height * $0.useHeight.2 * $0.yFactors.2
)
)
}
}