I am a begginer using Swift.
In the example I am following from the book ' iOS 14 Programming for Beginners', the instructions tell me to create a folder called Location and as sub folders create 2 other folders called Model and View. It then instructs me to right click on Location, new file (Cocoa Touch Class) and name the swift file LocationViewController and create the file.
According to the book, it should contain the following override function:
override func viewDidLoad() {
super.viewDidLoad()
}
Here is the file that is produced when I create the Cocoa Touch Class, where the above override is missing.
l
//
// LocationViewController.swift
// LetsEat2
//
// Created by Tony Hudson on 17/03/2021.
//
import UIKit
class LocationViewController: UICollectionViewCell {
}
As this is generated by Cocoa Touch, I can't see why this is missing?
Can anybody explain why this code is missing?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I am learning Xcode from Ahmad Sahar's book and somehow I have disconnected the exploreViewController from the navigation controller. I have now progressed far beyond the point where this was connected to the navigation controller, so I Ctrl + Dragged from the button to the navigation controller.
However when I ran the project I get a Compile error:
Couldn't compile connection: IBCocoaTouchOutletConnection: 0x123ef3a50 (sN1-9E-uLq) source=IBProxyObject: 0x123ef2be0 (Wnw-oV-gaS) 'Placeholder for UIStoryboardPopoverPresentationSegueTemplate with OID GlU-yO-pRa' property=anchorView destination=IBUIButton: 0x120195400 (WOO-Ms-Zlt) 'Button'
As I am still learning I am not sure how to fix this problem?
If I click on the segue icon it says 'Present as Popover segue'
and Segue Actions shows 'instantiation' with a circle but if I click on the circle nothing is listed for the segue.
Please help?
I am trying to learn Xcode using iOS 14 Programming for Beginners (Packt Pub).
I have had several problems with the code so I have been ultra careful and I can't find any mistakes.
There is also another error in the code in an extension in the code.
Here is a copy of the code:
/
// MapViewController.swift
// EatOut
//
// Created by Tony Hudson on 15/07/2021.
// Links to: Item -308,318,319,320,321,324,341,352,353
import UIKit
import MapKit
class MapViewController: UIViewController,MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let manager = MapDataManager()
var selectedRestaurant: RestaurantItem?
override func viewDidLoad() {
super.viewDidLoad()
initialize()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
switch segue.indentifier! { //<<Value of type 'UIStoryboardSegue' has no member 'indentifier'
case Segue.showDetail.rawValue: showRestaurantDetail(segue: segue)
default:
print("Segue not added")
}
}
}
// MARK: Private Extension
private extension MapViewController {
func initialize() {
mapView.delegate = self
manager.fetch {(annotations) in addMap(annotations)}
}
func addMap(_ annotations: [RestaurantItem]) {
mapView.setRegion(manager.currentRegion(latDelta: 0.5, longDelta: 0.5), animated: true)
mapView.addAnnotations(manager.annotations)
}
func showRestaurantDetail (segue: UIStoryboardSegue){
if let viewController = segue.destination as? RestaurantDetailViewController, let restaurant = selectedRestaurant {
viewController.selectedRestaurant = restaurant
}
}
}
// MARK: MKMapViewDelegate
extension MapViewController: MKMapViewDelegate { //<< Redundant conformance of 'MapViewController' to protocol 'MKMapViewDelegate'
func mapView(_ mapView: MKMapView, annotationView view:MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
guard let annotation = mapView.selectedAnnotations.first
else{
return
}
selectedRestaurant = annotation as? RestaurantItem
self.performSegue(withIdentifier: Segue.showDetail.rawValue, sender: self)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation ) -> MKAnnotationView? {
let identifier = "custompin"
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil
}
var annotationView: MKAnnotationView?
if let customAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
annotationView = customAnnotationView
annotationView?.annotation = annotation
} else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "custom-annotation")
}
return annotationView
}
}
I am wondering if there is some action that I have not done previously that is causing these errors.
Can anyone help?
I am trying to learn Xcode using a book called 'iOS 14
Programming for Beginners'. Although the book is supposed to show the correct
code I have found that it still raises errors when I type in the code. They
also have the code online, which I have downloaded and compared to my code, yet
I still get errors.
Here is the file that produces the errors:
//
// ExploreViewController.swift
// EatOut
//
// Created by Tony Hudson on 15/07/2021
//
import UIKit
class ExploreViewController: UIViewController,
UICollectionViewDelegate {
@IBOutlet weak var collectionView:
UICollectionView!
let manager = ExploreDataManager()
var selectedCity: LocationItem?
var headerView: ExploreHeaderView!
override func viewDidLoad() {
super.viewDidLoad()
initialize()
}
override func prepare(for segue:
UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case
Segue.locationList.rawValue:
showLocationList(segue: segue)
case
Segue.restaurantList.rawValue: //ERROR
HERE
showRestaurantListing(segue: segue)
default:
print("Segue not added")
}
}
override func
shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
{
if identifier ==
Segue.restaurantList.rawValue { //AND HERE
guard selectedCity!= nil else {
showAlert()
return false
}
return true
}
return true
}
}
// MARK: Private Extension
private extension ExploreViewController {
func initialize() {
manager.fetch()
}
func showLocationList(segue:
UIStoryboardSegue) {
guard let navController =
segue.destination as? UINavigationController, let viewController =
navController.topViewController as? LocationViewController else {
return
}
guard let city = selectedCity
else {
return
}
viewController.selectedCity = city
}
func showRestaurantListing(segue:
UIStoryboardSegue) {
if let viewController =
segue.destination as? RestaurantListViewController, let city = selectedCity,
let index = CollectionView.indexPathsForSelectedItems?.first {
viewController.selectedType = manager.explore(at: index).name
viewController.selectedCity = city
}
}
func showAlert() {
let alertController = UIAlertController(title:
"Location Needed", message: "Please select a location.",
preferredStyle: .alert)
let okAction =
UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController,
animated: true, completion: nil)
}
@IBAction func unwindLocationCancel(segue:
UIStoryboardSegue){
}
@IBAction func
unwindLocationDone(segue:UIStoryboardSegue) {
if let viewController =
segue.source as? LocationViewController {
selectedCity =
viewController.selectedCity
if let location =
selectedCity {
headerView.lblLocation.text = location.full
}
}
}
}
// MARK: UICollectionViewDataSource
extension ExploreViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView:
UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath:
IndexPath) -> UICollectionReusableView {
let header =
collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "header", for: indexPath)
headerView = header as?
ExploreHeaderView
return headerView
}
func collectionView(_ collectionView:
UICollectionView, numberOfItemsInSection section: Int) -> Int {
manager.numberOfItems()
}
func collectionView(_ collectionView:
UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier:
"exploreCell", for: indexPath) as! ExploreCell
let item = manager.explore(at:
indexPath)
cell.lblName.text = item.name
cell.imgExplore.image = UIImage(named:
item.image)
return cell
}
//
Can anyone see why this is producing this error?```
I am new to Xcode and have found a useful book online called 'Intro to app development with Swift. It is written for Xcode 10 but I have Xcode 12.4.
The instructions do not follow in the new version. In particular I want to add an image in the View Controller but I can't find anything like Image View in the Attributes Inspector. Can someone indicate. where this is so I can move on from the exercise I am doing.
Also Is there an upto date book online that can help me learn Xcode?