容器viewcontrollers与iPad应用程序上的单个自定义viewcontrollers

时间:2011-04-15 17:36:45

标签: xcode ipad nsnotificationcenter addsubview

我正在为iPad应用程序转换现有的iPhone应用程序。 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:方法,但在我的情况下,它似乎被调用,只是不可靠。< / p>

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

-Derrick

btw - 如果我将自定义viewController推送到我的app委托中的UINavigationController,它的工作原理应该是这样的。我只是不需要这个应用程序的导航控制器。

1 个答案:

答案 0 :(得分:0)

在我正在处理的应用程序中,我首先找一个属性来查看该设备是否为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 
    }   

}

希望这有帮助。

相关问题