代码之家  ›  专栏  ›  技术社区  ›  schaechtele

UideveOrientationDidChangeNotification将不会停止

  •  4
  • schaechtele  · 技术社区  · 15 年前

    我在使用uideviceorientationdidchangenotification时遇到一个小问题。在我离开某个viewController(viewwill消失:方法被调用)后,设备将不会停止发送通知。

    这意味着,在我将另一个viewcontroller推到堆栈顶部并旋转设备后,将调用此处viewcontroller的receivedRotate:方法,而这是我不想要的。

    我在文档和其他主题中也找不到任何内容。如果有人能帮忙,那就太好了。

    我的场景如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
         // other things...
    }
    

    此时将显示视图:方法

    - (void)viewWillAppear:(BOOL)animated {
    
        [super viewWillAppear:animated];
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    

    最后,视图将消失:方法

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    }
    

    事先谢谢!

    1 回复  |  直到 15 年前
        1
  •  12
  •   Alex Reynolds    15 年前

    -viewWillDisappear: ,移除观察者:

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];