Suppose you have a view:
struct Library: View {
@State var books = []
var body: some View {
VStack {
...
Button("add Book") {
....
}
}
}
Such that Library view holds a Book array that gets populated internally via a button that opens a modal – or something alike.
How can I use #Peviews to preview Library with a pre-populated collection? Suppose I have a static books – dummy – array in my code just for that; still, what's the best way to use that to preview the Library view?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
How do I gain access to the Documents folder? Under targets, "signing and capabilities", App Sandbox, I can see the Music folder, Desktop... but not Documents.
In order to setup a preview, I need to create a Book; to do that, I need to await a function – however, one cannot await inside a Preview:
import SwiftUI
struct BookView: View {
let book: Book
var body: some View {
VStack {
Image(book.thumbnail, scale: 1.0,
label: Text(book.title))
}
}
}
#Preview {
let url = URL(filePath: "/Users/dan/Documents/Curs confirmare RO.pdf")!
// 👇 here, `createBook` should be awaited; but how?
let book = createBook(for: url, of: CGSize(width: 254, height: 254), scale: 1.0)
BookView(book: book)
}
I have a SwiftUI view of the form
struct ContentView: View {
// ...
.onDrop(of: [.pdf], isTargeted: $isDropTargeted) { pdfs in
for pdf in pdfs {
I'm just not sure what to do next, I see there's a loadPreviewImage() that if I use like:
Task.detached {
// returns any NSSecureCoding object
let image = try! await pdf.loadPreviewImage()
}
Not sure how I'm supposed to get my preview image from that NSSecureCoding object
Topic:
UI Frameworks
SubTopic:
SwiftUI
I'm trying to create and advertise a Bonjour service via Network.framework.
In Xcode, target, Info tab, I've added the entry to plist:
And then written the following code:
guard let listener = try? NWListener(service: .init(name: "My Printer Service", type: "_printer._tcp"),using: .tcp) else {
return nil
}
self.listener = listener
listener.stateUpdateHandler = { newState in
switch newState {
case.ready:
print("starting")
case .failed(let error):
print("error: \(error)")
default:
print(newState)
}
}
listener.start(queue: .main)
However, the status is failed with error message:
POSIXErrorCode(rawValue: 22): Invalid argument
Apple Music's "Automatic Downalods" checkbox does not stay checked when saving settings and does not work at all.
Note this was broken for me for a long, long, time (Catalina?).
The code for @State doesn't seem to work.
struct DonutListView: View {
var donutList: DonutList
@State private var donutToAdd: Donut?
var body: some View {
List(donutList.donuts) { DonutView(donut: $0) }
Button("Add Donut") { donutToAdd = Donut() }
.sheet(item: $donutToAdd) { // <-- would need a "donut in"
TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped
Button("Save") {
donutList.donuts.append(donutToAdd)
donutToAdd = nil
}
Button("Cancel") { donutToAdd = nil }
}
}
}
Does anyone have a fix for this?
Thanks,
Dan!
I want to display a simple message while waiting for a list to be populated. Something like Loading... and when the list starts being populated with even one item, remove the message, via a transition, and display the list.
My current attempt, that does not work, looks like:
struct JoinChannelView: View {
@Binding var rooms: [ChannelInfo]
@State var selectedRoom: String?
var body: some View {
VStack {
if $rooms.isEmpty {
Text("Loading...")
.transition(.slide)
} else {
List(selection: $selectedRoom) {
ForEach(rooms, id: \.title) { room in
Text(room.title)
.font(.title)
Text(room.description)
.font(.subheadline)
}
.padding(.bottom)
}
}
}
.frame(width: 150, height: 300)
}
}
I want to have a single selection in a List, holding content of a custom struct:
struct ChannelInfo: Hashable {
let title: String
let description: String
}
And the view:
struct JoinChannelView: View {
@Binding var rooms: [ChannelInfo]
@State var selectedRoom: String?
var body: some View {
VStack {
if $rooms.isEmpty {
Text("Loading...")
.transition(.slide)
} else {
List(selection: $selectedRoom) {
ForEach(rooms, id: \.title) { room in
Text(room.title)
.font(.title)
Text(room.description)
.font(.subheadline)
}
.padding(.bottom)
}
}
}
.frame(width: 150, height: 300)
}
}
My problem here is, that because inside the ForEach I render two Text views, both are selectable, but that's incorrect, I only want the title, the first Text to be selectable.
In XCode, if I hit a breakpoint, I can't view the value of some local variables:
I've read about disabling optimisations for debugging, but that's already done
Currently NaviagitonView on macOS creates a side panel. And then navigationViewStyle does not support stack view for macOS.
Given this, is there currently a method to totally switch the content of a window from one view to another on macOS?
For iOS NavigationLinks provide a good way to completely transition from one "screen" (view) to another.
In macOS however, NavigationLinks don't completely switch the whole contents of a screen to another.
What I want to achieve is to make a Button press trigger a transition from one screen to another.