How to prevent the view from being viewed when a button is clicked (quick)


Maria

I have a button on a view that contains this condition:

 FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if let user = user {
            self.performSegue(withIdentifier: "AddDevice", sender: nil)
        }
        else {
            let alert = UIAlertController(title: "Sorry", message:"You Have to register", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
            self.present(alert, animated: true){}
        }
    }

Check if the user is logged in! If logged in, the segue should run and open the next view

If not, an alert should be displayed to the user.

But the next view contains a function that retrieves the current user data! (logged in user) in the viewDidLoad function!

So when the user is not logged in and clicks the button, I keep crashing instead of an alert!

How to prevent seeing the next view and just show the alert! So I can avoid the plane crashing?

if

From your description, it sounds like it's seguealways being executed, but that's not what you want.

Connect the segue from the view controller itself, name it, and put the whole method inside the IBActionconnection with the button .

like this:

@IBAction func loginDidTouch(sender: AnyObject) {
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if let user = user {
            self.performSegue(withIdentifier: "AddDevice", sender: nil)
         }
         else {
             let alert = UIAlertController(title: "Sorry", message:"You Have to register", preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in })
             self.present(alert, animated: true){}
    }
}

E.g:

  • In the first image, the segue is connected to the button. So every time the button is pressed it will execute segue.

  • In the second image, the segue is connected to the view controller, and the button is only connected to via IBAction. So the button only fires, actionbut the action fires segue.

  • It is easy to check by selecting the segue in the storyboard

button selected

vc selection

Related


How to prevent button from moving when clicked?

JustAnotherSimpleProgrammer__ I have the following code: <!DOCTYPE html> <html> <body> <head> <script> function Show(element){ element.style.display = "block"; } function Hide(element){ element.style.display = "none"; } function Slide(){ try{ img1 = im

How to prevent button from moving when clicked?

storm I have several buttons in a div that will display a value once clicked. The problem I'm having is: After a button is clicked and a value is displayed, it also moves down. I'm not sure how to prevent this from happening. Can someone help? Here is a link t

How to prevent button from moving when clicked?

JustAnotherSimpleProgrammer__ I have the following code: <!DOCTYPE html> <html> <body> <head> <script> function Show(element){ element.style.display = "block"; } function Hide(element){ element.style.display = "none"; } function Slide(){ try{ img1 = im

How to prevent button from moving when clicked?

JustAnotherSimpleProgrammer__ I have the following code: <!DOCTYPE html> <html> <body> <head> <script> function Show(element){ element.style.display = "block"; } function Hide(element){ element.style.display = "none"; } function Slide(){ try{ img1 = im

How to prevent button from moving when clicked?

JustAnotherSimpleProgrammer__ I have the following code: <!DOCTYPE html> <html> <body> <head> <script> function Show(element){ element.style.display = "block"; } function Hide(element){ element.style.display = "none"; } function Slide(){ try{ img1 = im

How to prevent button from moving when clicked?

JustAnotherSimpleProgrammer__ I have the following code: <!DOCTYPE html> <html> <body> <head> <script> function Show(element){ element.style.display = "block"; } function Hide(element){ element.style.display = "none"; } function Slide(){ try{ img1 = im

Vue JS how to prevent button from being clicked twice in a row

Pathum Kalhan I have a button that the user can click as many times as they want. However, when the user clicks the button, he may accidentally click twice, in which case the code should prevent the second click. If I explain further. The interval between clic

Vue JS how to prevent button from being clicked twice in a row

Pathum Kalhan I have a button that the user can click as many times as they want. However, when the user clicks the button, he may accidentally click twice, in which case the code should prevent the second click. If I explain further. The interval between clic

Prevent radio button from being fully clicked

Dimitri This has been bugging me for a while. I am trying to prevent the user from clicking a radio button depending on whether the user has permission to do so. The first solution is to do the following: Disabled <input type="radio" name="my_radio1" id="abc1"

Prevent radio button from being fully clicked

Dimitri This has been bugging me for a while. I am trying to prevent the user from clicking a radio button depending on whether the user has permission to do so. The first solution is to do the following: Disabled <input type="radio" name="my_radio1" id="abc1"