【问题标题】:container viewcontrollers versus single custom viewcontrollers on iPad app容器视图控制器与 iPad 应用程序上的单个自定义视图控制器
【发布时间】:2011-04-15 17:36:44
【问题描述】:

我正在将现有的 iPhone 应用程序转换为 iPad 应用程序。 iPhone 应用程序是使用容器视图控制器 (UINavigationController) 构建的,它首先向用户展示了一个自定义视图控制器 (UITableViewController),该控制器根据行选择推送了一个自定义视图控制器 (UIViewController)。

在 iPad 应用程序中,我直接向用户展示了自定义 UIViewController(没有容器控制器),然后允许通过 UIPopoverController 选择不同的选项。在 myAppDelegate.m 中,我只是使用以下方法将自定义 UIViewController 添加到窗口:

[window addSubview:[myCustomViewController view]];

在 myCustomViewController.m 中,我通过在 viewWillAppear 中注册方向更改通知来大量修改视图:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)                                                  name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

然后我在 didRotate: 方法中测试方向并得到非常奇怪的结果。仅仅加载视图就被调用了三遍?它似乎还报告了与视图的 PREVIOUS 绘图相对应的方向?

- (void) didRotate:(NSNotification *)notification
{   
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
        NSLog(@"Portrait");
    } else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape");
    }
}

我正在阅读文档,似乎将子视图添加到窗口(没有容器类)不会导致调用 viewWillAppear: 方法,但在我的情况下,它似乎正在被调用,只是不可靠。

我应该为这个应用程序使用其他模式吗?我只想加载一个自定义视图并使用两个弹出框控制器(没有其他导航)?

-井架

btw - 如果我将自定义 viewController 推送到我的应用程序委托中的 UINavigationController 上,它会完全正常工作。我只是不需要这个应用程序的导航控制器。

【问题讨论】:

    标签: xcode ipad nsnotificationcenter addsubview


    【解决方案1】:

    在我正在开发的应用程序中,我首先有一个属性来确定设备是否是 iPad:

    - (BOOL)iPad {
    
        return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO;
    }
    

    然后您可以使用以下视图的委托方法。

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    
    if (self.iPad) {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
            toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        //do some stuff 
        }   
    

    }

    希望这会有所帮助。

    【讨论】:

    • 杰米-谢谢。由于初始视图明显不同,我在单独的 AppDelegate 中处理设备检查时涉及到设备检查。我将尝试您概述的委托方法,而不是 NSNotificationCenter。当前的 NSNotificationCenter 方法适用于容器控制器,只是没有它。再次感谢您的回复和帮助。
    • Jamie - 我只是尝试放弃 NSNotification 方法并使用 willRotateToInterfaceOrientation 方法。问题仍然存在,但我认为它是自定义 drawrect 代码和没有容器控制器的组合。如果我只是嵌套在 UINavigationController 中,一切都会正常工作。再次感谢您帮助我解决这个问题。
    猜你喜欢
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-06-11
    • 2021-12-31
    相关资源
    最近更新 更多