sag
sag

Reputation: 186

How to use properly Container View Controller on iOS

My App have one Parent View Controller(MainViewController.h/m - UIViewController, without NIB file) for all other UIViewControllers, which is at the same time RootViewController.

My App should support iOS 5, so AutoLayout is off..

Hier some code:

In 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;
}

In 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 contains 7 UIButtons. If one of them touched MainViewController Class is being called to change from one ChildViewController(HomeViewController, etc..) to another.

HomeViewController.m

- (IBAction)firstButton_click:(id)sender {

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

Now about the Problem.

On iPhone(Device or Simulater) with iOS 6 and above UIButtons respond only after several touches. Buttons placed on the bottom of the view to work, must be touched more times, then buttons placed on the top. After several touches, when event fired and view was changed, when I come back to this View everything work normally. I have this problem only with iPhone iOS 6.x. It works normally on iPhone iOS 5.x and iPad 5.x-6.x.

If I make HomeViewController RootViewController, Buttons of course respond to events. But then I can't transform with UIViewAnimationOptionTransitionCrossDissolve Animation[[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:], because view must have same Parent View Controller. What did I wrong? Is it bug? Is there any solution? Any help would be greatly appreciated.

Upvotes: 0

Views: 557

Answers (2)

kagmanoj
kagmanoj

Reputation: 5368

Write this in .pch file

#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];
}

Upvotes: 2

sag
sag

Reputation: 186

The Problem was because of [_homeViewController didMoveToParentViewController:self];. It is superfluous.

ViewDidLoad in MainViewController should be:

- (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];
}

Now everything works..

Upvotes: 0

Related Questions