ViewWillAppear没有使用UISplitViewController调用

S B*_*S B 7 objective-c ipad ios

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

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

window
`-- splitVC (UISplitViewController)
    `-- rootNav (UINavigationController)
        `-- hvc (HostManagerViewController, derived from UIViewController)
    `-- detailViewController (DetailViewController, derived from UIViewController)
Run Code Online (Sandbox Code Playgroud)

这在App Delegate中实现如下:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

viewWillAppear被调用的时候都DetailViewController.mHostManagerViewController.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
Run Code Online (Sandbox Code Playgroud)

但是当我改变HostManagerViewController代码时

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

Info.plist中仅支持横向模式

编辑:插入NSLog消息进行跟踪shouldAutorotateToInterfaceOrientation,viewWillAppearViewDidLoad

ACB*_*urk 8

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

#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
Run Code Online (Sandbox Code Playgroud)

您必须检查版本号,以便不进行两次viewDidAppear调用.

或者如果可以的话,只需使用5.0或更高版本,因为错误似乎已修复.