0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftUIのプレビューで@EnvironmentObjectの値を設定する

Posted at

モデルクラス

DialogModel.swift
import Foundation

public enum DialogType {
    case none
    case loading
}

class DialogModel: NSObject, ObservableObject {
    @Published var dialogType: DialogType = .none
}

SwiftUI ContentView

ContentView.swift
import SwiftUI

struct ContentView: View {
    
    @EnvironmentObject var dialogModel: DialogModel
    
    var body: some View {

プレビューするときにEnvironmentObject内の変数の値を設定する方法

ContentView.swift
struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        Group {

            ContentView().environmentObject({ () -> DialogModel in
                let envObj = DialogModel()
                envObj.dialogType = .none
                return envObj
            }() )

            ContentView().environmentObject({ () -> DialogModel in
                let envObj = DialogModel()
                envObj.dialogType = .loading
                return envObj
            }() )

        }
    }
}

参考

GitHub

TDD

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?