Does anyone know what this stack is called SwfitUI? [closed] - swift

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 months ago.
Improve this question
I'll keep it quick: Does anyone know if SwiftUI have a built in method that renders something like this image:
where the view just changes based on what label you tap? I wonder if it's possible to achieve this using some sort of navigation view or stack? I'd appreciate any input! Thanks.
EDIT:
HStack {
Picker(selection: $selected, label: Text("Mode"), content:{
Text("Projects").tag(1)
Text("Notes").tag(2)
Text("Docus").tag(3)
}).pickerStyle(SegmentedPickerStyle())
.frame(width: 200, height: 10)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: -0))
if selected == 1 {
// how would i show another view if the user selects option 1?
}
}

This is a picker. to be precise, this is a segmented picker.
You can create it like so:
struct ContentView: View {
#State private var favoriteColor = 0
var body: some View {
Picker("What is your favorite color?", selection: $favoriteColor) {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
}
.pickerStyle(.segmented)
}
}
When we create the picker we pass in a binding (when we change the picker's value it will know to switch to it) this is the thing with the dollar sign ($)
The next thing is to add the segments.
So we add text views with a tag attached to each one.
Lastly we need to set the picker style (in this case the segmented)
.pickerStyle(.segmented)
I suggest you to look here: Create a segmented control and read values from it
Hope this helps!

Related

How to persist state from a class into a separate SwiftUI view? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am having problems persisting a class's state into a SwiftUI struct's view.
I have a class that acts as a controller, defined in one file, and a SwiftUI view that is supposed to change according to properties in that controller.
I've defined these files as such:
ClockController.swift
class ClockController:ObservableObject {
#Binding var isAM:Bool
init(){
self.isAM = false
}
func toggleAMPM(){
self.isAM = !self.isAM
}
}
and TestUI.swift
struct TestUI:View{
#ObservedObject var clockController:ClockController = ClockController()
var body: some View {
Button(action: {
self.clockController.toggleAMPM()
}){
Text("Toggle")
}
Text(self.clockController.isAM ? "AM" : "PM")
}
}
I want the TestUI to change/re-render every time the self.clockController.isAM variable changes (when the toggle button is pressed), which is why I have made ClockController an ObservableObject and added the #Binding keyword to the isAM property. However, I keep getting the following errors with this setup on ClockController's initializer method:
'self' used in property access 'isAM' before all stored properties are initialized and Return from initializer without initializing all stored properties
How can I get my TestUI to bind on ClockController's isAM variable?
all! I've fixed the problem myself. I needed to remove the #Binding keyword from ClockController's isAM property and change it to #Published. That did the trick.
Kudo's to YouTube and this video: https://youtu.be/-yjKAb0Pj60?t=919

SwiftUI on Mac: Help Text Always Visible Even within View with Zero Opacity

I have run into some unexpected behavior while using SwiftUI in a macOS app. I filed a Feedback with Apple in case it's a bug, but it might actually be designed to work this way, so I'm looking for a workaround.
I rely heavily on the use of .opacity() to show and hide different sections of my app with tabs. I don't use if clauses because each time the user changes the tab, you have to wait for the entire view to rebuild and that is pretty slow.
Here's a basic example that demonstrates the problem:
struct ContentView: View {
#State var viewAVisible = false
var body: some View {
VStack{
ZStack{
Text("View A Visible")
.frame(width: 500, height: 500)
.background(Color.blue)
.help("This is View A's help text. It should be invisible when View A is invisible.")
.opacity(viewAVisible ? 1 : 0)
Text("View B Visible")
.frame(width: 500, height: 500)
.background(Color.gray)
.opacity(viewAVisible ? 0 : 1)
}
Button("Toggle"){
viewAVisible.toggle()
}
}.padding()
}
}
The default app state is to hide the "View A" Text() and only show the "View B" Text(). But if you hover over View B, you still see View A's .help text:
In my opinion, if a view has .opacity(0) then its help text shouldn't show up. But regardless, I need to find a way around this.
I thought about doing something like this:
.help(viewAVisible ? "This is View A's help text..." : "")
...but that doesn't scale across dozens of views in my app--particularly among child views that don't know if their parent view is shown or hidden. As I mouse across my app, I see the help text of tons of views all over the place even though they are invisible. 😅
Has anyone run into this or have any suggestions on how to handle it?
Looks like a bug (they do not remove tracking rects), here is a demo of workaround - move help tag into background and remove it manually (tested with macOS 12.0.1)
Text("View A Visible")
.frame(width: 500, height: 500)
.background(Group {
if viewAVisible {
Color.blue.help("This is View A's help text. It should be invisible when View A is invisible.")
} else {
Color.clear
}
})
.opacity(viewAVisible ? 1 : 0)

what's an efficient way in swift to get a "change of day" event [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
so I've a swift-ui app where I show information for the current day - so at midnight I want it to switch to a new day - trouble is I want this to be as "light" as possible - eg I don't want anything consuming any resources as all I want to do is refresh the view, if it's displayed.
How do I do this without consuming background resources when the view isn't shown?
you could try something like this:
(note I did not wait to see if this works)
import SwiftUI
import Combine
#if os(iOS)
import UIKit
#endif
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Text("day change")
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.significantTimeChangeNotification)) { _ in
print("----> day time has changed <----\n")
print("A notification that posts when there is a significant change in time, \n for example, change to a new day (midnight), \n carrier time update, and change to or from daylight savings time.")
}
}
}

Unable to use ForEach loop on #Binding variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I recently asked a question however it was removed as they thought the answer existed already and so I checked out this question. I have tried implementing the solution found here into my code and now run into a slightly different error.
Basically I am trying to create a simple application with a list of items that can be clicked on to navigate to a detail view with an edit feature. The data is read from a JSON file which is why the data is in a binding so it can be updated and this is not a problem anywhere else in the project.
import SwiftUI
struct EateryList: View {
#Binding var eateries: [Eatery]
var body: some View {
NavigationView {
VStack {
List {
ForEach(eateries.indicies, id: \.self) { i in
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { //errors appear here
EateryRow(eatery: $eateries[i])
}
}
}
.navigationTitle("Favourite Eateries")
.navigationBarItems(leading: EditButton(), trailing: Button( action: add)
{
Image(systemName: "plus")
}
)
.listStyle(InsetGroupedListStyle())
}
}
}
func add() {
eateries.append(Eatery(name: "Eatery", location: "Insert location here", notes: "Insert notes here", reviews: ["Insert reviews here"], url: "https://i.imgur.com/y3MMnba.png"))
}
}
I get 3 errors with this code currently which I do not fully understand...
Generic struct 'ForEach' requires that 'Binding' conform to 'RandomAccessCollection'
Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<[Eatery]>'
Value of type '[Eatery]' has no dynamic member 'indicies' using key path from root type '[Eatery]'
All help is greatly appreciated right now and if you need further clarification of other parts of the project please let me know :)
It's a spelling mistake. It's indices not indicies
ForEach(eateries.indices, id: \.self) { i in //<-- Here
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) {
EateryRow(eatery: $eateries[i])
}
}

button click counter blackberry cascades [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to make an app that count how many times you've clicked the button in blackberry 10
so i'm trying to int a value that will increase by 1 when you click the button
import bb.cascades 1.2
Page {
Container {
ImageButton {
defaultImageSource: "asset:///item_XS_6405840_3762396.jpg"
onClicked: {
count.setText(count + 1)
}
onTouch: {
count.setText(count + 1)
}
}
Label {
text: "you've clicked:"
}
Label {
id: count
text: 0
}
Button {
id: reset
text: "reset"
onClicked: {
count = 0;
}
}
}
}
I only have knowledge in java. thanks for helping