Reputation: 211
I'm trying to run a function when the app finish loading up, like viewDidLoad
, but I'm using SwiftUI now and I have no viewDidLoad
. How can I do this now?
var body: some View {
NavigationView {
Form {
Section {
self.exampleFunction()
Text(" ...... ")
}
}
}
}
I want to take some information from that function and present it in the text. But the way I'm doing it is wrong. It's not building.
Upvotes: 21
Views: 34740
Reputation: 1693
I'm using init()
instead. I think onApear()
is not an alternative to viewDidLoad()
. Because onApear is called when your view is being appeared. Since your view can be appear multiple times it conflicts with viewDidLoad
which is called once.
Imagine having a TabView
. By swiping through pages onApear() is being called multiple times. However viewDidLoad() is called just once.
struct TeamInfoView: View {
var myText:String
init() {
myText = function()
}
.....
}
After having initialized it's time to use the value of the string:
var body: some View {
NavigationView {
Form {
Section {
Text(myText)
}
}
}
}
Upvotes: 0
Reputation: 4465
Fully updated for Xcode 11.2, Xcode 5.0
I think the viewDidLoad()
just equal to implement in the body closure.
SwiftUI gives us equivalents to UIKit’s viewDidAppear()
and viewDidDisappear()
in the form of onAppear()
and onDisappear()
. You can attach any code to these two events that you want, and SwiftUI will execute them when they occur.
As an example, this creates two views that use onAppear()
and onDisappear()
to print messages, with a navigation link to move between the two:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Hello World")
}
}
}.onAppear {
print("ContentView appeared!")
}.onDisappear {
print("ContentView disappeared!")
}
}
}
Upvotes: 14
Reputation: 161
If you're trying to run something after the app launches and has nothing to do with a specific view you can add code in two different places...
In AppDelegate.swift, the first function is called after the App launches...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// ******** Run your function here **********
return true
}
Or in, SceneDelegate.swift, the first function actually sets the root view to the original ContentView...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Use a UIHostingController as window root view controller
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
// ******** Add code here before root view is shown **********
window.makeKeyAndVisible()
// ******** Add code here after root view is shown **********
}
}
Upvotes: 9
Reputation: 878
You can use .onAppear { ... }
to execute arbitrary code when a view appears:
var body: some View {
NavigationView {
Form {
Section {
Text(" ...... ")
}.onAppear { self.exampleFunction() }
Upvotes: 27