Как реализовать функцию наподобие onAppear в SwiftUI? - PullRequest
0 голосов
/ 05 апреля 2020

Итак, я хотел бы создать собственный вид и добавить функцию. Как я могу реализовать такую ​​функцию, как .onAppear(perform: (() -> Void)?)? Мой код не работает, onDismiss закрытие не вызывает в DashboardView.

struct DashboardView: View {

  @State var employees = ["Alex", "Olga", "Mark"]
  @State var presentEmployeeView = false

  var body: some View {
    NavigationView {
      List {
        Section {
          Button(action: {
            self.presentEmployeeView = true
          }, label: {
            Text("All employees")
          }).buttonStyle(BorderlessButtonStyle())
        }
      }
    }
    .sheet(isPresented: $presentEmployeeView) {
      EmployeesView(employees: self.employees).onDismiss {
        self.presentEmployeeView = false
      }
    }
  }
}

struct EmployeesView: View {
  let employees: [String]

  @State private var onDismissClosure: (() -> Void)? = nil
  func onDismiss(perform action: (() -> Void)? = nil) -> some View {
    self.onDismissClosure = action
    return self
  }

  var body: some View {
    NavigationView {
      List {
        ForEach(employees) { employee in
          EmployeeCell(employee: employee)
        }
      }.navigationBarItems(leading:
        Button(action: {
          self.onDismissClosure?()
        }, label: {
          Text("Close")
       })
      )
    }
  }
}

1 Ответ

0 голосов
/ 05 апреля 2020

Здесь возможен подход. Протестировано и работало с Xcode 11.4 / iOS 13.4

struct DashboardView: View {

  @State var employees = ["Alex", "Olga", "Mark"]
  @State var presentEmployeeView = false

  var body: some View {
    NavigationView {
      List {
        Section {
          Button(action: {
            self.presentEmployeeView = true
          }, label: {
            Text("All employees")
          }).buttonStyle(BorderlessButtonStyle())
        }
      }
    }
    .sheet(isPresented: $presentEmployeeView) {
      EmployeesView(employees: self.employees) {
        self.presentEmployeeView = false
      }
    }
  }
}

struct EmployeesView: View {
  let employees: [String]
  var onDismiss = {}

  var body: some View {
    NavigationView {
      List {
        ForEach(employees, id: \.self) { employee in
          Text("\(employee)")
        }
      }.navigationBarItems(leading:
        Button(action: {
          self.onDismiss()
        }, label: {
          Text("Close")
       })
      )
    }
  }
}

Обновление: возможный вариант использования с модификатором:

    ...
    .sheet(isPresented: $presentEmployeeView) {
      EmployeesView(employees: self.employees).onDismiss {
        self.presentEmployeeView = false
      }
    }
  }
}

struct EmployeesView: View {
  let employees: [String]
  var onDismiss = {}

  func onDismiss(_ callback: @escaping () -> ()) -> some View {
    EmployeesView(employees: employees, onDismiss: callback)
  }

  var body: some View {
    NavigationView {
      List {
        ForEach(employees, id: \.self) { employee in
          Text("\(employee)")
        }
      }.navigationBarItems(leading:
        Button(action: {
          self.onDismiss()
        }, label: {
          Text("Close")
       })
      )
    }
  }
}
...