我正在寫檢查設備的初始物理位置的應用程序的麻煩。如果是平的,設備是應該做的事,然後用戶應該以定向裝置在其他位置,這樣應用程序可以做額外的工作。如果該應用是不平坦的,則用戶然後應該定向位置,使得它是平的,並繼續再移動設備在不同的位置由該應用的要求。經與如果iOS的應用程序中使用UIDeviceOrientation/else語句
無論設備最初是否平坦,或者用戶必須移動設備以使其平坦,這兩種操作都應該調用相同的方法,導致一系列需要的if/else語句處理。這裏是該裝置的初始物理方位,檢查方法:
- (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是一組枚舉的,其值每一次必須改變的裝置的取向改變。每次設備更改位置時,都會對總共三個測試(平面,橫向和縱向測試)執行新的測試。上面的代碼塊調用的方法是這樣的:
- (void) orientationChanged:(NSNotification *)note {
if (note == nil) {
device = [UIDevice currentDevice];
}
else
device = note.object;
我出於某種原因的代碼沒有達到低於
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」,或者,我不能因爲某些原因連接前兩個if/else語句在orientationChanged方法的if/else語句是來之後。
非常感謝您的建議。儘管基本一樣,但它幫助我找到了問題,並且後來找到了解決方案。你能發表一個答案,以便我可以選擇它嗎? – syedfa
可以在正式關閉原因下關閉:_此問題是由於無法再現的問題或簡單的印刷錯誤導致的。雖然類似的問題可能在這裏討論,但這個問題的解決方式不太可能有助於未來的讀者。你會考慮關閉它嗎? – halfer