UINavigationController 呈现空白视图

发布于 2024-09-27 23:01:22 字数 1092 浏览 0 评论 0原文

我在 iPhone 上遇到了一个非常奇怪的 UINavigationController 问题,我正在用头撞墙。

其要点是我正在执行对服务器的调用,当该调用失败时,我将当前视图替换为包含错误消息的视图。使用 PerformSelectorOnMainThread 在主线程上调用有问题的代码。

实际情况是,在设备上大约有一半的时间会显示空白屏幕。在模拟器上,它每次都会呈现一个空白屏幕,这让我认为这可能是某种计时问题,由于模拟器中的处理速度更快,该问题更加突出。如果我通过单击 ui 中的按钮来显示页面来调用相同的函数,则效果非常好,所以我不认为代码本身有问题。

我已经验证我要添加的控制器位于导航堆栈中。已验证它在主线程上被调用,它是可见的,帧大小和位置正确。我尝试显式地将视图设置为可见,将其移动到其父视图的前面并调用 setNeedsDisplay ,甚至手动调用drawRect。这些都不起作用。

对这里可能发生的事情有什么想法吗?我假设它与运行循环有关,但我无法弄清楚。非常感谢您的帮助。有问题的相对简单的代码如下

 UINavigationController* navController = self.navigationController; 

 int count = [navController.viewControllers count];

 NSMutableArray* controllers = [[NSMutableArray alloc] initWithCapacity:count];

 for (int i=0; i<count; i++) {
  if (self == [self.navigationController.viewControllers objectAtIndex:i]) {
   [controllers addObject:newController];
  }
  else {
   [controllers addObject:[self.navigationController.viewControllers objectAtIndex:i]];
  }

 }
 [self.navigationController setViewControllers:controllers animated:YES];
 [controllers release]; 

I have a very strange problem with UINavigationController on the iphone and I am banging my head against the wall on this.

Gist of it is I am executing a call to a server and when that call fails I swap out the current view with a view containing an error message. The code in question is called on the main thread by using performSelectorOnMainThread

What happens in practice is that on the device it shows a blank white screen about half the time. On the simulator it presents a blank screen every time leading me to think this is perhaps some kind of timing problem that is more prominent due to better processing speeds in a simulator. This works perfectly if I call the same function by clicking a button in the ui to display the page so I don't think its a problem with the code itself.

I have verified that the controller I am adding is in the navigation stack. Verified it is being called on the main thread, it is visible, the frame size and location are correct. I have tried explicitly setting the view to be visible, moved it to the front in its parent view and called setNeedsDisplay and even manually called drawRect. None of this works.

Any thoughts on what could be going on here? I am assuming it has something to do with the run loop but I can't figure it out. Help would be much appreciated. The relatively simple code in question is below

 UINavigationController* navController = self.navigationController; 

 int count = [navController.viewControllers count];

 NSMutableArray* controllers = [[NSMutableArray alloc] initWithCapacity:count];

 for (int i=0; i<count; i++) {
  if (self == [self.navigationController.viewControllers objectAtIndex:i]) {
   [controllers addObject:newController];
  }
  else {
   [controllers addObject:[self.navigationController.viewControllers objectAtIndex:i]];
  }

 }
 [self.navigationController setViewControllers:controllers animated:YES];
 [controllers release]; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-10-04 23:01:22

我真的不明白你在那里做什么。这样的事情行不通?

- (void)displayMyErrorVC {
    MyErrorVC *errorVC = [[[MyErrorVC alloc] init] autorelease];          
    [self.navigationController pushViewController:errorVC animated:YES];
}

然后在另一个线程中,如果出现错误:

[self performSelectorOnMainThread:@selector(displayMyErrorVC) withObject:nil waitUntilDone:NO];

I really don't understand what you're doing there. Something like this won't work?

- (void)displayMyErrorVC {
    MyErrorVC *errorVC = [[[MyErrorVC alloc] init] autorelease];          
    [self.navigationController pushViewController:errorVC animated:YES];
}

And then in your other thread, if you have an error:

[self performSelectorOnMainThread:@selector(displayMyErrorVC) withObject:nil waitUntilDone:NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文