iPhone定位问题

iPhone定位问题,iphone,objective-c,Iphone,Objective C,下面是我的代码,当iPhone旋转时调用 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { switch (interfaceOrientation) { case UIInterfaceOrientationPortrait: // some action return YES; cas

下面是我的代码,当iPhone旋转时调用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
switch (interfaceOrientation) {
    case UIInterfaceOrientationPortrait:
                    // some action
        return YES;
    case UIInterfaceOrientationLandscapeLeft: {
                    // some action
        return NO;
    }
    case UIInterfaceOrientationLandscapeRight: {
                    // some action
        return NO;
    }
    default:
        return NO;
        break;
}
}

我以为这就够了,但后来我发现

case UIInterfaceOrientationPortrait:
从来没有人打过电话。
我能做什么?我必须使用默认值吗?也许这不是最好的解决方案,或者如果您只想使用纵向方向,那么这应该足够了


你在哪里重写了这个函数?它应该在最顶层的视图控制器中。

我刚刚将您的代码提供给了一个示例应用程序,它工作正常,使用给定的代码,当我将方向更改为横向时,视图中任何东西的方向都不会发生任何变化。一种方法是为每个方向分别设计视图,并对所有方向返回“是”,并使用以下属性

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) 
    interfaceOrientation duration:(NSTimeInterval)duration { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait 
        || interfaceOrientation == 
            UIInterfaceOrientationPortraitUpsideDown) { 
       //design your portrait view eg: for button, button.frame = CGRectMake(x,y,w,h)
    } 
    else { 
        //design for the landscape
    } 
} 

嗯。我找到了答案。当您旋转手机时会收到通知

    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector:@selector(receivedRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object: nil];

}


使用它;)

这是我最上面的视图控制器。。。每次我换成风景画的时候都叫它,但当我换回肖像画的时候就不叫它了。。。我不知道为什么:(它怎么能变为横向,你正在返回否,所以它不能。这可能就是为什么它在返回到纵向时不打电话的原因,因为你确保它始终是纵向的。在变为横向时尝试返回是,然后看看是否打了纵向电话。我不想变为横向,我只是在手机关机时做一些动作。)anges改为横向,但我不想把整个视图改为横向,只是一些图标。
- (void)receivedRotate:(NSNotification*)notif {
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
switch (interfaceOrientation) {
    case UIInterfaceOrientationPortrait: {
        break;
    }
    case UIInterfaceOrientationLandscapeLeft: {
        break;
    }
    case UIInterfaceOrientationLandscapeRight: {
        break;
    }
    default:
        break;
}