How to Change This View

How do I change the root view with SwiftUI

You just display each view conditionally in a root view

var body: some View {
if isLoggedIn {
HomeView()
} else {
LoginView() {
}
}

Change view via ViewModel

Thanks to Gorky's respond I figured out how to do that.

In Fragment I created observer

sharedViewModel.changeView.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
if (hasFinished) nextFragment()
})

I created changeView variable in ViewModel. When

 var changeView = MutableLiveData<Boolean>()

change to true, observer call function.

source:
https://developer.android.com/codelabs/kotlin-android-training-live-data#6

How to change view on midnight in WidgetKit, SwiftUI?

  1. Assuming you have an Entry (in your app you have entry.state... but for this example I used a simplified version):
struct SimpleEntry: TimelineEntry {
let date: Date
let lesson: Lesson
}

  1. Setup your TimelineProvider to refresh timeline after the next midnight:
struct SimpleProvider: TimelineProvider {
...

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let currentDate = Date()
let midnight = Calendar.current.startOfDay(for: currentDate)
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!

let entries = [
SimpleEntry(date: currentDate, lesson: Lesson()) // pass the lesson here
]

let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}

In the TimelineProvider you may pass any lesson you want (depending on the day or the previous lesson - it's up to you). You may also pass a variable to an Entry indicating whether the lesson is completed.

By setting the .after(nextMidnight) policy you indicate when do you want your Timeline (and therefore you Widget View) to be reloaded.

React How to change view after click on button

There are multiple ways to do this. You can set flags around the views. For example

render(){
return(
<div className="button-toolbar">
<button className="button" onClick={() => setState({view: 1})}>Button 1</button>
<button className="button" onClick={() => setState({view: 2})}>Button 2</button>
<button className="button">Button 3</button>

{this.state.view === 1 ? <View1></View1> : ''}
{this.state.view === 2 ? <View2></View2> : ''}
<View3></View3>
</div>
)

Changing views via conditional statements inside of async commands in SwiftUI IOS15

SwiftUI renders your user interface in response to changes in state. So instead of trying to render Dashboard or ContentView within your Task, instead you need to set some form of state higher up that determines which view to show to the user.

For example, if you had a @State variable recording your logged in status, your code would start to look like

struct ContentView: View {
@State var loggedIn = false

var body: some View {
if loggedIn {
Dashboard()
} else {
// login form
Button {
Task {
let logInStatus = try await network.LoginFunction(userName: username, passWord: password)
self.loggedIn = logInStatus
}
}
}
}
}

When your async tasks changes the ContentView's state, SwiftUI's rendering subsystem will pick up that loggedIn has changed, and re-render body so that the login form is replaced by a call to Dashboard().



Related Topics



Leave a reply



Submit