Having trouble with if/else statements using UIDeviceOrientation in iOS application [closed]

StackOverflow https://stackoverflow.com/questions/14179844

質問

I'm writing an application that checks a device's initial physical position. If it is flat, the device is supposed to do something, and the user is then supposed to orient the device in other positions so that the app can do additional work. If the app is NOT flat, the user is then supposed to orient the position so that it is flat, and continue to then move the device in various positions as requested by the app.

Regardless of whether the device is initially flat, or if the user has to move the device so that it's flat, both actions are supposed to call the same method that lead to a series of if/else statements that need to be processed. Here is the method where the initial physical orientation of the device is checked:

- (void) accelerometerCheck {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

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

        currentTest++;
        [self orientationChanged:nil];

    }

    else {

        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationChanged:)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:[UIDevice currentDevice]];


    }


}

currentTest is a set of enums whose value must change each time the orientation of the device changes. Each time the device changes position, a new test is performed for a total of three tests (flat, landscape, and portrait tests). The method that the above code block calls looks like this:

- (void) orientationChanged:(NSNotification *)note {

    if (note == nil) {

        device = [UIDevice currentDevice];
    }

    else
        device = note.object;

My code for some reason is not reaching this 'if' statement below

  if(currentTest == flatTest) {

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

            //do something 

        }

        else {

          //do something other stuff

        }


    }
    // If the device is initially in the flat position, then it should
    // call the method "orientationChanged" and come directly to this
    // point in the method...which it is not doing. 
    else if (currentTest == landscapeTest) {


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

            //do some tests


        }

        else {


        }

        currentTest++;

    }

    else if (currentTest == portraitTest) {

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

            //do more tests

        }

        else {

            return;

        }

        currentTest++;
    }

    else if(currentTest == doneTest) {

       //give test results to the user

        }

        else

            [self testFail];

    }

    else if(currentTest == timeoutTest) {

        //Do something to tell the user the test timed out


    }

}

Can anyone see what I am doing wrong? My feeling is that I am either not passing the right value for the NSNotification parameter to the method "orientationChanged", or, I am unable for some reason connect the first two if/else statements in the orientationChanged method to the if/else statements that come after.

役に立ちましたか?

解決

Try to put Break point in your each Condition and See for Each Change in device Orientation how The Coding working.Then you can easily find where is the fault.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top