ViewWillAppear没有使用UISplitViewController调用 [英] ViewWillAppear not getting called with UISplitViewController

查看:111
本文介绍了ViewWillAppear没有使用UISplitViewController调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景&目标:我有一个基于UISplitViewController的iPad应用程序 - 到现在它支持4个方向但现在我想将其锁定为仅景观。
我更改了左视图控制器的 shouldAutorotateToInterfaceOrientation 以仅支持横向模式,但这会停止其 viewWillAppear 被叫。

Background & Objective: I have a UISplitViewController based iPad app - till now it supported 4 orientations but now I want to lock it down to only landscape. I changed shouldAutorotateToInterfaceOrientation of the left view controller to support only landscape mode, but this stops its viewWillAppear from getting called.

详细信息:我的iPad的视图控制器组织如下:

Details: My iPad's view controllers are organized as below:

window
`-- splitVC (UISplitViewController)
    `-- rootNav (UINavigationController)
        `-- hvc (HostManagerViewController, derived from UIViewController)
    `-- detailViewController (DetailViewController, derived from UIViewController)

这是在App Delegate中实现的,如下所示:

This is implemented in the App Delegate as below:

- (BOOL)application:(UIApplication *)application
         didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    HostManagerViewController *hvc  = [[[HostManagerViewController alloc]
                                       initWithNibName:nil bundle:nil] autorelease];
    self.detailViewController = [[[DetailViewController alloc]
                                initWithNibName:nil bundle:nil] autorelease];
    UINavigationController *rootNav = [[[UINavigationController alloc]
                                        initWithRootViewController:hvc] autorelease];
    UISplitViewController *splitVC= [[[UISplitViewController alloc] init] autorelease];
    [splitVC setViewControllers:[NSArray arrayWithObjects:rootNav,
                                 detailViewController, nil]];

    splitVC.delegate = detailViewController;
    [window addSubview:splitVC.view];
    [window setRootViewController:splitVC];
    return YES;
}

viewWillAppear 被调用当 DetailViewController.m HostManagerViewController.m 包含

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
Console output:
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Hostmanager: Viewdidload
Should rotate called to hostmanager with 1
Hostmanager: viewwillappear

但当我将 HostManagerViewController 的代码更改为

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

然后调用HostManagerViewController的'viewWillAppear`。控制台输出

then 'viewWillAppear` of HostManagerViewController is not invoked. Console output

Should rotate called to hostmanager with 1 (1 is the numeric value of interfaceOrientation)
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Hostmanager: Viewdidload
Should rotate called to hostmanager with 1

使用信息管理器

编辑:插入NSLog消息以跟踪 shouldAutorotateToInterfaceOrientation viewWillAppear ViewDidLoad

Inserted NSLog messages to track shouldAutorotateToInterfaceOrientation, viewWillAppear and ViewDidLoad

推荐答案

我问你使用的iOS版本因为我试图创建一个简单的版本再现您的问题,发现4.3和5.0之间的行为不同。它实际上确实在5.0中进行了所有正确的调用。我相信这只是UISplitviewController的一个错误。 Splitview控制器很不错。 aopsfan的答案并没有解决问题(我已经证实了这一点)。我建议继承uisplitviewcontroller,并覆盖splitview控制器的viewwillappear,viewdid似如下:

I asked what iOS version you were using because I tried to create a simple reproduction of your problem and found the behavior differs between 4.3 and 5.0. It actually does make all the correct calls in 5.0. I believe this to simply be a bug with the UISplitviewController. Splitview controller was quite buggy. aopsfan's answer does not solve the problem (I have confirmed this). I would suggest subclassing uisplitviewcontroller, and overriding the splitview controller's viewwillappear, viewdidappear like so:

#import "testSplitViewController.h"

@implementation testSplitViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSString *reqSysVer = @"5.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] == NSOrderedAscending)
        [[[self viewControllers] objectAtIndex:0] viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSString *reqSysVer = @"5.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] == NSOrderedAscending)
        [[[self viewControllers] objectAtIndex:0] viewDidAppear:animated];
}
@end

你必须检查版本号,以便你不要让viewDidAppear调用两次。

You have to check the version number so that you don't make the viewDidAppear call twice.

如果可以的话,或者只使用5.0或更高版本,因为这个bug似乎已修复。

Or just go with 5.0 or above if you can, since the bug seems to be fixed.

这篇关于ViewWillAppear没有使用UISplitViewController调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆