Search code examples
iosif-statementwhile-loopuideviceorientation

How to go through sequential steps inside a while loop?


I need to go through a series of steps one by one. In all, I have three steps to go through which are inside of a while loop. Once the three tests are completed, then and only then should the user be exited from the while loop. The catch is that these steps need to be done sequentially, and require the user to do each test in order, if they pass, then move on to the next step.

Here is the relevant code:

int passCount = 0;
    BOOL flatPass = FALSE;
    BOOL landscapePass = FALSE;
    BOOL portraitPass = FALSE;

while (passCount < 3) {


        if (flatPass == FALSE) {

            if (device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown) {

                [self pushSound];

            }

        }


        else if (landscapePass == FALSE) {

            if (device.orientation == UIDeviceOrientationLandscapeLeft || device.orientation == UIDeviceOrientationLandscapeRight) {

                [self pushSound];

            }

        }


        else if (portraitPass == FALSE) {

            if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {

                [self pushSound];

            }

        }

    }

I need the user to position the iOS device in each position, and a beep sound is played to indicate a successful test. Once ALL of the three tests have been completed in order, I want the user to be exited from the loop. I figure each time a test has been cleared, I would increment the passCount counter by 1, until we reach 3 which would exit me from the loop. My issue though is how to go through each test, and in order.


Solution

  • Assuming this isn't running on the main UI thread, remove the while loop, replace each if and else if with a while condition, set the appropriate boolean flags to true when a test passes and you're done.