Reputation: 1980
I have a view which sometimes is used to browse data and sometimes to select data.
When used to browse, it is presented by a NavigationLink.
When used to select data, it is presented as a modal sheet and closed by setting binding boolean isPresented to false, so I can make use of sheet function onDismiss.
On browse mode, however, I need a way to skip initializing isPresented binding boolean. The right way would be an optional argument on view init() but all what I try throws an error.
This is the way I call it for browsing data:
NavigationLink(destination:BrowseOrSelectView(selMode:SelModes.browse)) {
Text("Browse")
}
This is the way I call it to select data:
.Sheet(isPresented: self.$isPresented, onDismiss:{...}) {
BrowseOrSelectView(selMode: SelModes.selection, isPresented: self.$isPresented)
}
This is the view:
struct BrowseOrSelectView: View {
@State var selMode:SelModes
@Binding var isPresented:Bool
public init(selMode: SelModes, isPresented:(Binding<Bool>)? = true) {
UITableView.appearance().separatorStyle = .none
_selMode = State(initialValue: selMode)
}
...
}
The error thrown is:
Cannot convert value of type 'Bool' to expected argument type Binding Bool
Upvotes: 3
Views: 745
Reputation: 257693
Use .constant binding, like
public init(selMode: String, isPresented:(Binding<Bool>) = .constant(true)) {
UITableView.appearance().separatorStyle = .none
_selMode = State(initialValue: selMode)
_isPresented = isPresented
}
Upvotes: 4