Using setViewController from UINavigationController on iPhone does not behave correctly

I have a problem with iPhone App using UINavigationController. When I use pushNavigationController, it works fine. The iPhone does its animation by switching to the next ViewController. But when using the ViewControllers array and the setViewControllers method, it crashes in the animation, which can grow into a clearly visible animation error.

The following snippet is called in the root ViewController. Depending on the state, it should either switch to ViewController1, or it should go directly to ViewController2. In the latter case, the user can return to vc1, and then to the root.

NSMutableArray* viewControllers = [NSMutableArray arrayWithCapacity:2];
// put us on the stack
[viewControllers addObject:self];
// add first VC
AuthentificationViewController* authentificationViewController =
  [[[AuthentificationViewController alloc] initWithNibName:@"AuthentificationViewController" bundle:nil] autorelease];
[viewControllers addObject:authentificationViewController];

if (someCondition == YES)
{
 UserAssignmentsListViewController* userAssignmentsListViewController =
      [[[UserAssignmentsListViewController alloc] initWithNibName:@"UserAssignmentsOverviewViewController" bundle:nil] autorelease];

 [viewControllers addObject:userAssignmentsListViewController];
}

[self.navigationController
  setViewControllers:[NSArray arrayWithArray:viewControllers] animated:YES];

, , , VC , navigationController . , . , , "". , . , , , . .

pushViewController, / . , , pushViewController. , VC, . , ​​? pushNavController VC2, - VC1 , .

.: -)

: iOS 4.2, 4.0.

+3
1

, . , , . UserAssignmentsListViewController viewDidLoad, , , (: UIButton). .

, , . , VC :

// initialize our top-level controller
ViewController* viewController2 = [[[ViewController alloc]
    initWithNibName:@"ViewController" bundle:nil] autorelease];

VC , ( ):

NSMutableArray* viewControllers = [NSMutableArray arrayWithCapacity:2];
// put us on the stack, too
[viewControllers addObject:self];

ViewController* viewController1 = [[[ViewController alloc]
    initWithNibName:@"ViewController" bundle:nil] autorelease];
[viewControllers addObject:viewController1];

if (someCondition == YES)
{
    [viewControllers addObject:viewController2];
}

[self.navigationController
    setViewControllers:[NSArray arrayWithArray:viewControllers] animated:YES];
+5

Source: https://habr.com/ru/post/1778315/


All Articles