简体   繁体   English

popViewControllerAnimated:YES不起作用

[英]popViewControllerAnimated:YES doesn't work

I'm writing a unit test and I find popViewControllerAnimated:YES doesn't work. 我正在编写单元测试,但发现popViewControllerAnimated:YES不起作用。

(void)testNavi {
  UINavigationController *navi = [[UINavigationController alloc] init];
  UIViewController *controllerA = [[UIViewController alloc] initWithNibName:nil bundle:nil];
  UIViewController *controllerB = [[UIViewController alloc] initWithNibName:nil bundle:nil];
  [navi pushViewController:controllerA animated:NO];
  [navi pushViewController:controllerB animated:NO];
  [navi popViewControllerAnimated:YES];
  XCTAssertEqual(navi.topViewController, controllerA);
}

If I change [navi popViewControllerAnimated:YES] into [navi popViewControllerAnimated:NO], it works. 如果我将[navi popViewControllerAnimated:YES]更改为[navi popViewControllerAnimated:NO],则可以使用。 I don't know why. 我不知道为什么

You'd better to call popViewControllerAnimated after view was created. 创建视图后,最好调用popViewControllerAnimated。 So you may make subclass of NavigationController and implements it like this. 因此,您可以制作NavigationController的子类并像这样实现它。

- (void)init {
   self = [super init];
   if (self) {
      UIViewController *controllerA = [[UIViewController alloc] initWithNibName:nil bundle:nil];
      UIViewController *controllerB = [[UIViewController alloc] initWithNibName:nil bundle:nil];
      self.viewControllers = @[controllerA, controllerB];
   }
   return self;
}

- (void)viewWillAppear {
   [self popViewControllerAnimated:YES];
}

This is because the animation happens asynchronously on another thread. 这是因为动画是异步发生在另一个线程上的。 The animation to pop the view controller takes a small amount of time, but the test assertion is made before the animation has completed, so the topViewController has not changed yet. 弹出视图控制器的动画需要花费少量时间,但是测试断言是在动画完成之前进行的,因此topViewController尚未更改。

The best way to test this it to mock the UINavigationController and verify that popViewController:YES is called. 测试它的最佳方法是模拟UINavigationController并验证是否调用了popViewController:YES

I had a similar problem testing pushViewController when the push is animated. 推送动画时,我在测试pushViewController时遇到了类似的问题。

I was able to solve this by wrapping my assertion in this handy delay function from matt. 通过将断言包装在来自matt的此便捷delay函数中,我能够解决此问题。

When introducing a delay in your test, it is necessary to use expectations to ensure the assertions are checked. 在测试中引入延迟时,有必要使用期望值以确保检查了断言。 Otherwise, the test completes before the delay code executes, resulting in a passed test that did not check your assertions! 否则,测试将在延迟代码执行之前完成,从而导致通过的测试未检查您的断言!

// code to push the view controller goes here...

// set up the expectation
let expectation = expectationWithDescription("anything you want")

// now, assert something about the navigation controller 
// after waiting 1/10th second for the animation to finish
delay (0.1) {

    // for example: Navigation controller should have the 
    // expected number of view controllers
    XCTAssertEqual(1, navigationController.viewControllers.count)

    // if we get this far, then assertions passed and we must
    // fulfill expectations to pass the test
    expectation.fulfill()
}

// wait longer than the delay value, so the code in the delay closure
// has time to finish
waitForExpectationsWithTimeout(0.2, handler: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [self.navigationController popViewControllerAnimated:YES]; 在委托回调中不起作用 - [ self.navigationController popViewControllerAnimated:YES ]; doesn't work in delegate callback popViewControllerAnimated:不想设置动画 - popViewControllerAnimated: doesn't want to animate popViewControllerAnimated不会更新信息iPhone SDK - popViewControllerAnimated doesn't update info iPhone SDK didUpdateValueForCharacteristic(setNotifyValue:YES)不起作用 - didUpdateValueForCharacteristic(setNotifyValue:YES) doesn't work 使用popViewControllerAnimated导致应用崩溃 - App Crashing using popViewControllerAnimated:YES 后退按钮出现在popViewControllerAnimated:YES - Back button appears on popViewControllerAnimated:YES popViewControllerAnimated:不会从内存中释放视图控制器 - popViewControllerAnimated: doesn't release view controller from memory 为什么在调用popViewControllerAnimated时ARC不释放内存 - Why ARC doesn't release memory when popViewControllerAnimated is called 在popViewControllerAnimated之后,UINavigationController不会释放/销毁控制器吗? - UINavigationController doesn't release/destroy controllers after popViewControllerAnimated? 使用append:YES with operation.outputStream AFHTTPRequestOperation不起作用 - Using append:YES with operation.outputStream AFHTTPRequestOperation doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM