I am using SwiftUI and I have added Button in NavigationBar but I am unable to set action on that button. I tried these two approaches but failed.
Approach:1
.navigationBarTitle("\(task.label)")
.navigationBarItems(trailing: UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.actionName)))
But I am having this error here
Argument type 'UIBarButtonItem' does not conform to expected type 'View'
Approach:2
.navigationBarTitle("\(task.label)")
.navigationBarItems(trailing: NavigationBarButtonItem())
struct NavigationBarButtonItem : View {
var body : some View {
Button(action: {
print("Button Tapped")
}, label: {Text("Done")})
}
}
Over here, This print statements never runs, although Button "Done" shows on Right side of NavigationBar, but action never works.