SwiftUI simple Pop-Up Alert from a function in a class
Here is a basic demo of activating an alert with a Button in a View body from an ObservableObject
class.
struct ContentView: View {
// However you've initialized your class, it may be different than this.
@StateObject var progress = DeliveriesViewModel()
var body: some View {
VStack {
// Other views in your View body
// SwiftUI button to activate the Bool in ObservableObject class.
Button {
progress.isDisplayingAlert.toggle()
} label: {
Text("Toggle Alert")
}
}
.alert(isPresented: $progress.isDisplayingAlert) { () -> Alert in
Alert(title: Text("Alert Title"), message: Text("Body of Alert."), dismissButton: .cancel())
}
}
}
class DeliveriesViewModel: ObservableObject {
@Published var isDisplayingAlert = false
func displayAlert() {
// other function actions...
// update Published property.
isDisplayingAlert = true
// other function actions...
}
}
How to deselect a list button within a NavigationLink
The issue here is the Text
which is above the List
in SettingsView - a bug reported here.
Instead, you can use a native navigation title and attach it to the TabView.
struct SettingsHomeView: View {
@State var startDayNotificationSetting: String = "8:30AM"
@State var appVersion: String = "0.01"
var body: some View {
// no `Text` above `List`
List {
NavigationLink(destination: SettingsStartdayView()){
HStack {
Text("Start Day Notification")
Spacer()
Text(startDayNotificationSetting)
.font(.subheadline)
.foregroundColor(Color.gray)
.multilineTextAlignment(.trailing)
//Image(systemName: "chevron.right")
}
}
}
}
}
struct ContentView: View {
@State private var isSelectedTab = 1 // select the first tab
var body: some View {
NavigationView{
TabView(selection: $isSelectedTab) {
// ...
}
// control displaying the title depending on the `isSelectedTab`
.navigationTitle("Settings")
.navigationBarHidden(isSelectedTab == 1)
}
}
}
Related Topics
Remembering Scroll Position on Uitableview
How to Stop Uitableview from Clipping Uitableviewcell Contents in iOS 7
iPhone Uitextfield Background Color
Showing a Uipickerview with Uiactionsheet in iOS8 Not Working
Ios: How to Copy HTML into the Cut-Paste Buffer
Can't Find Pods.Modulemap - Looking in Wrong Directory
Uicollectionview Scrolling Choppy When Loading Cells
Swift 3 Objc Optional Protocol Method Not Called in Subclass
Using Collectionview in Uiview with Xib File
How to Fill a Bezier Path with Gradient Color
Convert String to Date Type in Swift 3
Url Scheme "Open Settings" iOS
Modifying Ekparticipants (Attendees) in Eventkit Like Sunrise
Xcode 4.3: Codesign Operation Failed (Check That the Identity You Selected Is Valid)
How to Check If a Framework Is Bitcode Supported for Xcode7
Why Does Safari Mobile Have Trouble Handling Many Input Fields on iOS 8