如何在iOS上正确使用Container View Controller

时间:2013-04-10 20:01:03

标签: iphone view ios6 containers

我的应用程序有一个父视图控制器(MainViewController.h / m - UIViewController,没有NIB文件)用于所有其他UIViewControllers,它们同时是RootViewController。

我的应用程序应支持iOS 5,因此AutoLayout已关闭..

一些代码:

在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainViewController = [[MainViewController alloc] init];
    [self.window setRootViewController:self.mainViewController];
    [self.window makeKeyAndVisible];
    return YES;
}

在MainViewController.m中

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view.


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if ([[UIScreen mainScreen] bounds].size.height == 568) {
            _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil];
        } else {
            _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil];
        }
    } else {
        _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    }


    [self addChildViewController:_homeViewController];
    [self.view addSubview:_homeViewController.view];
    [_homeViewController didMoveToParentViewController:self];
}

- (void)changeFromViewController:(UIViewController*)fromViewController toViewController:(UIViewController*)toViewController withDuration:(NSNumber*)duration {

    toViewController.view.frame = self.view.bounds;
    [toViewController.view layoutIfNeeded];

    [self addChildViewController:toViewController];
    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:[duration floatValue]
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:nil
                            completion:^(BOOL finished) {

                                [toViewController didMoveToParentViewController:self];

                                [fromViewController willMoveToParentViewController:nil];
                                [fromViewController removeFromParentViewController];
                            }];
}

HomeViewController * .xib包含7个UIButtons。如果其中一个触及了MainViewController类,则调用它从一个ChildViewController(HomeViewController等等)更改为另一个。

HomeViewController.m

- (IBAction)firstButton_click:(id)sender {

    [(MainViewController *)self.parentViewController setAnimationForChangeFrom:self toStartTestSettingsViewControllerWithDuration:[NSNumber numberWithDouble:0.4] andWithDelay:[NSNumber numberWithDouble:0.1]];
}

现在关于问题。

在使用iOS 6及更高版本的iPhone(设备或模拟器)上,UIButtons仅在多次触摸后才会响应。按钮放置在视图底部工作,必须触摸更多次,然后按钮放在顶部。经过几次触摸,当事件被触发并且视图被更改时,当我回到此视图时,一切正常。我只有iPhone iOS 6.x才有这个问题。它在iPhone iOS 5.x和iPad 5.x-6.x上正常工作。

如果我制作HomeViewController RootViewController,按钮当然会响应事件。但后来我无法使用UIViewAnimationOptionTransitionCrossDissolve Animation [[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]进行转换,因为视图必须具有相同的父视图控制器。 我错了什么?是bug吗?有什么解决方案吗? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

将此内容写入.pch文件

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.



if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    if (IS_IPHONE_5) {
        _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil];
    } else {
        _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil];
    }
} else {
    _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
}


[self addChildViewController:_homeViewController];
[self.view addSubview:_homeViewController.view];
}

答案 1 :(得分:0)

问题是因为[_homeViewController didMoveToParentViewController:self] ;. 这是多余的。

MainViewController中的ViewDidLoad应为:

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view.


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if ([[UIScreen mainScreen] bounds].size.height == 568) {
            _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone5" bundle:nil];
        } else {
            _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController~iPhone" bundle:nil];
        }
    } else {
        _homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    }


    [self addChildViewController:_homeViewController];
    [self.view addSubview:_homeViewController.view];
}

现在一切正常..