iPad 应用程序上的容器视图控制器与单个自定义视图控制器

发布于 2024-11-01 10:14:25 字数 1555 浏览 0 评论 0原文

我正在将现有的 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: 方法中测试方向并得到非常奇怪的结果。只是加载视图就被调用了三次?它似乎还报告了与先前绘制的视图相对应的方向?

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

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

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

-Derrick

顺便说一句 - 如果我将自定义 viewController 推送到我的应用程序委托中的 UINavigationController 上,它的工作原理就完全一样。我只是不需要这个应用程序的导航控制器。

I am converting an existing iPhone app for the iPad app. The iPhone app was built using a container viewcontroller (UINavigationController) which presented the user first with a custom viewcontroller (UITableViewController) that pushed a custom viewcontroller (UIViewController) based on row selection.

In the iPad app, I am presenting the user directly with the custom UIViewController (with NO container controller) and then allow selection of different options via a UIPopoverController. In myAppDelegate.m I am simply adding the custom UIViewController to the window using:

[window addSubview:[myCustomViewController view]];

In myCustomViewController.m I am modifying the view heavily based upon device rotation by registering for orientation change notifications in viewWillAppear:

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

I am then testing the orientation in the didRotate: method and getting very strange results. It is being called three times simply loading the view? It also seems to be reporting the orientation corresponding to the PREVIOUS drawing of the view?

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

I was reading in the docs and it appears that adding the subview to the window (without a container class) will not cause viewWillAppear: method to be called, but in my case it seems it is being called, just unreliably.

Is there some other pattern I should be using for this app? I simply want to load a single custom view and use two popover controllers (no other navigation)?

-Derrick

btw - It works exactly as it should if I push the custom viewController onto a UINavigationController in my app delegate. I just don't need a nav controller for this app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2024-11-08 10:14:25

在我正在开发的应用程序中,我首先有一个属性来查明设备是否是 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 
    }   

希望

这有帮助。

In my app I'm working on, I first have a property to find out if the device is an iPad:

- (BOOL)iPad {

    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO;
}

And then you can use the following delegate method of your view.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (self.iPad) {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    //do some stuff 
    }   

}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文