开发者社区 问答 正文

关于iphone中事件没有被调用 的问题

在应用中有几个UIViewController。其中一个willAnimateRotationToInterfaceOrientation 事件调用成功了。

如何让willAnimateRotationToInterfaceOrientation对所以可见视图有效?

展开
收起
爵霸 2016-03-24 08:16:27 1886 分享 版权
1 条回答
写回答
取消 提交回答
  • RootViewController中:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        NSDictionary *userInfo = @{
            @"toInterfaceOrientation":@(toInterfaceOrientation),
            @"duration":@(duration)};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:nil userInfo:userInfo];
    }

    其他的 UIViewControllers:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willAnimateRotationToInterfaceOrientation:) name:@"willAnimateRotationToInterfaceOrientation" object:nil];
        }
        return self;
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(NSNotification *)notification {
        UIInterfaceOrientation toOrientation = (UIInterfaceOrientation)[notification.userInfo[@"toOrientation"] intValue];
        NSTimeInterval duration = (UIInterfaceOrientation)[notification.userInfo[@"duration"] floatValue];
        [self willAnimateRotationToInterfaceOrientation:toOrientation duration:duration];
    }
    
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
        // ...
    }
    
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
    }
    2019-07-17 19:11:46
    赞同 展开评论
问答分类:
问答标签:
问答地址: