首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS应用程序中使用UIDeviceOrientation的if/else语句有问题

在iOS应用程序中使用UIDeviceOrientation的if/else语句有问题
EN

Stack Overflow用户
提问于 2013-01-06 14:16:13
回答 1查看 278关注 0票数 0

我正在编写一个应用程序来检查设备的初始物理位置。如果它是平的,设备应该做一些事情,然后用户应该将设备定位到其他位置,这样应用程序就可以做额外的工作。如果应用程序不是平坦的,则用户应该将位置调整为平坦,然后根据应用程序的要求继续在不同的位置移动设备。

无论设备最初是否是扁平的,或者如果用户必须移动设备使其扁平,这两个操作都应该调用相同的方法,这些方法会导致一系列需要处理的if/else语句。以下是检查设备的初始物理方向的方法:

代码语言:javascript
复制
- (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是一组枚举,每次设备的方向改变时,它们的值都必须改变。每次设备改变位置时,将执行一次新的测试,总共进行三次测试(平面、横向和纵向测试)。上面的代码块调用的方法如下所示:

代码语言:javascript
复制
- (void) orientationChanged:(NSNotification *)note {

    if (note == nil) {

        device = [UIDevice currentDevice];
    }

    else
        device = note.object;

由于某种原因,我的代码没有达到下面的'if‘语句

代码语言:javascript
复制
  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


    }

}

有没有人看到我做错了什么?我的感觉是,我没有将正确的NSNotification参数值传递给方法" orientationChanged ",或者由于某种原因,我无法将orientationChanged方法中的前两个if/else语句连接到后面的if/else语句。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-08 14:16:44

尝试在每个条件中设置断点,并查看对于设备方向的每个更改,您可以轻松地找到错误所在的编码working.Then。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14179844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档