Hi everyone,
I am studying SwiftUI on the Apple tutorial - https://developer.apple.com/tutorials/swiftui/creating-a-macos-app.
In the Creating a MacOS App chapter in section 3 Update a Row View, there is a following instruction: Step 6 Choose the WatchLandmarks target to see a watchOS preview of the list.
How to do this?
It should be easy and simple, but somehow I haven't found it.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Created
Hi everyone,
I'm trying to follow the Landmarks project tutorial and got stuck in the second part, in the second section (Create the Row View), step 6 where it says "Modify the text view to use the landmark property’s name." This must be done in the LandmarkRow.swift file.
https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation
The tutorial shows that after doing this, the preview will display the name of the landmark (Turtle Rock) instead of the usual "Hello world" greeting. But my replacement is not happening. Moreover, from this point on, the preview stops working. There are no error messages in the code, except for the message that the preview could not be performed.
I checked and rewrote the code several times, replaced the data source files, but nothing helped.
At the same time, the preview works well in other view files.
I can't figure out what's wrong with my code? Any ideas as to what the reason will be is appreciated.
Below is the code of two files, the LandmarkRow.swift, where view does not work, the second is ModelData.swift and it is related to the previous one.
LandmarkRow.swift
import SwiftUI
struct LandmarkRow: View {
var landmark: Landmark
var body: some View {
HStack {
landmark.image
.resizable()
.frame(width: 50, height: 50)
Text(landmark.name)
Spacer()
}
}
}
struct LandmarkRow_Previews: PreviewProvider {
static var previews: some View {
LandmarkRow(landmark: landmarks[0])
}
}
ModelData.swift
import Foundation
var landmarks: [Landmark] = load("landmarkData.json")
func loadT: Decodable(_ filename: String) - T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
Hi everyone,I try to pass data from View Controller through Tab Bar Controller and Navigation Controller to Table View Controller. Screenshot of storyboard is bellow.https://drive.google.com/open?id=1GW6gaDxfK1zaEOk2aPE8izkS5B67ldZLMy code is next:1. Send data from View Controller@IBAction func addToCarButton(_ sender: UIButton) {
tabBarController?.selectedIndex = 1
let navVC = tabBarController?.viewControllers![1] as! UINavigationController//
let cartTableViewController = navVC.topViewController as! CartTableViewController
cartTableViewController.titleItem = titleLabel.text
cartTableViewController.image = SomeImage(photo: imageView.image!)
}2. Get data in Table View Controllerimport UIKit
class CartTableViewController: UITableViewController {
// MARK: - Store data
// Create variables for receiving data (title and data from image) from VC
var titleItem: String?
var image: SomeImage?
// Create shopping cart - array of selected items
var cartItems = [Cart]()
var cartItem: Cart?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
//Load data from archive file
cartItems = Cart.loadFromFile()
// Create a new item
if titleItem != nil, image != nil {
print("titleItem in Cart View Appear - \(String(describing: titleItem))")
cartItem = Cart(title: titleItem!, image: image!)
}
// Add new item to shopping cart
if cartItem != nil {
cartItems.append(cartItem!)
}
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
// Pass title to cell
cell.textLabel?.text = cartItems[indexPath.row].title
// Retrieve image from array of archive file and convert from Data to UIImage format
let image = UIImage(data: cartItems[indexPath.row].image.photo)
// Pass image to cell:
cell.imageView?.image = image
return cell
}
// MARK: - Save data to archieve file
override func viewWillDisappear(_ animated: Bool) {
Cart.saveToFile(cartItems: cartItems)
tableView.reloadData()
}
// Clear cart
@IBAction func clearCartButton(_ sender: UIBarButtonItem) {
cartItems.removeAll()
tableView.reloadData()
}But data are appear only after second click. I.e., I add item 1 and cart is empty. When I add item 2, cart has item 1. If then I add item 3, card has item 1 and item 2, and go on.I have done link bellow on gif that shows how it works.https://drive.google.com/open?id=1_ilwKHXTRBT250zB4At7UsbWJ09fQARlI tried to use different options, but I can not understand what is happening here.Could you give me advice how to fix it?