在用户更改到另一个选项卡之前强加一个需要满足的条件

发布于 2025-01-03 10:23:00 字数 179 浏览 4 评论 0原文

我遇到的情况是,我需要确保用户在移动到 UITabBarController 内的另一个选项卡之前已完成某些步骤。因此,如果用户正在执行某些操作并点击另一个选项卡,我想显示一个 UIAlertView ,说明“在转到另一个选项卡之前,您必须完成等等”。

是否可以检查此条件并取消移动到另一个视图控制器?

I have situation where I need to make sure that user has completed certain steps before they move to another tab inside UITabBarController. So if the user is in middle of something and taps on another tab, I would like to show a UIAlertView saying "you must complete blah blah blah before you go to another tab."

Is it possible to to check this condition and cancel moving to another view controller?

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

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

发布评论

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

评论(2

若能看破又如何 2025-01-10 10:23:00

当然可以。我想您的标签栏控制器位于 AppDelegate 类中。如果是这样,请将 AppDelegate 设置为其委托。然后执行下面的方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  // place all the checks here
  EditingViewController *editingController = //link to controller where editing is being made.
  if (editingController && editingController.isEditing) {
    //UIAlertView
    return NO;
  }
  return YES;
}

Sure you can. I suppose you have your tabbar controller in the AppDelegate class. If so, set the AppDelegate to be its delegate. Then implement the following method

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  // place all the checks here
  EditingViewController *editingController = //link to controller where editing is being made.
  if (editingController && editingController.isEditing) {
    //UIAlertView
    return NO;
  }
  return YES;
}
挖个坑埋了你 2025-01-10 10:23:00

猜测您可以尝试捕获正在退出的视图,并将选项卡栏控制器上的选定索引更改为您希望保留它们的视图:

- (void)viewWillDisappear:(BOOL)animated {
   self.tabBarController.selectedIndex = 0;
}

您可能会发现这有点不稳定,但根据事件的顺序,快速谷歌发现,如果你可以让你的视图控制器成为 UITabBarControllerDelegate 那么你可以实现:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

这将允许你更早地捕获它们。您可能会发现在应用程序委托中实现此操作并让它知道(或检查)是否应该允许更改是最简单的。

At a guess you could try catching the view on it's way out and changing the selected index on the tab bar controller to be the view you wish to keep them on:

- (void)viewWillDisappear:(BOOL)animated {
   self.tabBarController.selectedIndex = 0;
}

You might find that's a bit jerky though depending on the order of events, a quick google has found that if you can make your view controller a UITabBarControllerDelegate then you can implement:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Which would allow you to catch them earlier. You might find it simplest to implement this in your App Delegate and have it know (or check) if it should allow the change away.

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