if statement in if statement

Hello! i was wondering how to add an if statement in an if statement. Here is my code:

At the top:

buttonPressed = true;

Below override func

if buttonPressed {

        Text.isHidden = false;

        buttonPressed = false;

        print("Text is now visible!")

        

        print("button has been pressed 1 time!")

        

    if buttonPressed {
        Text2.isHidden = false;
        print("button has been pressed 2 times!")

    }

Thanks!

Answered by Claude31 in 706413022

Look, you can never display second message:

1. if buttonPressed {
2.         Text.isHidden = false;
3.         buttonPressed = false;
4.         print("Text is now visible!")
5.         print("button has been pressed 1 time!")
6.     if buttonPressed {
7.         Text2.isHidden = false;
8.         print("button has been pressed 2 times!")
9.     }
  • line 3: buttonPressed is set to false
  • then line 6 the test fails

But could you show the complete code ? Is it inside an IBAction ?

I understand you want to detect that button was pressed a second time ?

If so you could change code as follows:

     buttonPressed = false // <<-- no need for ; at the end of statement in Swift
     @IBAction func testButton(_ sender: UIButton) {
          if !buttonPressed {                       // At start button was not pressed
               text.isHidden = false                // Text is a var, should start with lowercase
               buttonPressed = true                 // Could also use buttonPressed.toggle()
               print("Text is now visible!")
               print("button has been pressed 1 time!")
          }
          if buttonPressed {                  // Button was pressed a first time
               text2.isHidden = false         // Text2 is a var, should start with lowercase
               print("button has been pressed 2 times!")
          }
     }
Accepted Answer

Look, you can never display second message:

1. if buttonPressed {
2.         Text.isHidden = false;
3.         buttonPressed = false;
4.         print("Text is now visible!")
5.         print("button has been pressed 1 time!")
6.     if buttonPressed {
7.         Text2.isHidden = false;
8.         print("button has been pressed 2 times!")
9.     }
  • line 3: buttonPressed is set to false
  • then line 6 the test fails

But could you show the complete code ? Is it inside an IBAction ?

I understand you want to detect that button was pressed a second time ?

If so you could change code as follows:

     buttonPressed = false // <<-- no need for ; at the end of statement in Swift
     @IBAction func testButton(_ sender: UIButton) {
          if !buttonPressed {                       // At start button was not pressed
               text.isHidden = false                // Text is a var, should start with lowercase
               buttonPressed = true                 // Could also use buttonPressed.toggle()
               print("Text is now visible!")
               print("button has been pressed 1 time!")
          }
          if buttonPressed {                  // Button was pressed a first time
               text2.isHidden = false         // Text2 is a var, should start with lowercase
               print("button has been pressed 2 times!")
          }
     }
if statement in if statement
 
 
Q