Home > Enterprise >  hide a button conditionally inside a if-statement
hide a button conditionally inside a if-statement

Time:12-21

I'm fairly new to coding so this question may be a duplicate but I can't seem to wrap my head around it.

The problem is that I want to hide the "Request Permission" button after permission has been granted by the user. My code is following

VStack {
                Button("Request Permission") {
                    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                        if success {
                            print("All set!")
                                .isHidden
                        } else if let error = error {
                            print(error.localizedDescription)
                            
                        }
                    }
                }

The error-code i get is the following: Value of tuple type '()' has no member 'isHidden'

I hope that you may be able to help me-
Thank you in advance.

CodePudding user response:

You could to hold the button's visibility in @State.

struct SwiftUIView: View {
@State var buttonIsVisible: Bool = true

var body: some View {
    VStack {
        if buttonIsVisible {
            Button("Request Permission") {
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                    if success {
                        buttonIsVisible = false
                        print("All set!")
                    } else if let error = error {
                        print(error.localizedDescription)
                        
                    }
                }
            }
        }
    }
}

CodePudding user response:

I don't really understand what you wanted but if you just want to hide and show the button after the button clicked with the status success or not you could just do like below:

import SwiftUI
import SwiftUIX

struct Authenticate: View {

   @State var isAuthenticated: Bool = false

   var body: some View {
       VStack {
           Button(action: {
               UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                   if success {
                       self.isAuthenticated = success
                   } else if let error = error {
                       print(error.localizedDescription)
                   }
               }
           }, label: {
               Text("Request Permission")
                   .padding()
                   .background(authenticateButtonBackground)
                   .hidden(isAuthenticated)
           })
           .disabled(isAuthenticated)
       }
   }

   var authenticateButtonBackground: some View {
       RoundedRectangle(cornerRadius: 10)
           .stroke(Color.black, lineWidth: 1)
   }
}

Just note that .hidden(isAuthenticated) is from SwiftUIX library and also note that I add .disabled(isAuthenticated) to stop user from clicking it even it already hidden, anyway you can go and check the library, there many good stuff in that library that can help you in handy way. I hope this is what you're looking for.

  • Related