iPhone:保存并验证返回导航

发布于 2024-08-15 12:37:35 字数 341 浏览 7 评论 0原文

在我的 iPhone 应用程序中,我有导航控制器、主屏幕和一些编辑屏幕。在编辑屏幕上,用户执行一些输入,在我保存之前必须验证这些输入。理想情况下,我希望在返回导航上自动更新数据,而不需要额外的“完成”按钮。我可以进行一些验证并保存后退导航(即当用户点击标准后退按钮时),以便在出现问题时允许我停止导航并显示一些错误消息吗?

我看到了其他几种可能性:

  • 创建我的自定义左按钮并使其看起来像标准背面。 (为什么苹果不把这个按钮样式放入公共 API 中?)
  • 添加“完成”按钮并仅在用户点击它时保存数据

,但这两个选择我都不喜欢。因此,如果有一种方法可以实现我想要的目标,我愿意使用它。

In my iPhone application I have navigation controller, main screen and some edit screens. On edit screen user does some input that has to be validated before I can save it. Ideally I would like to update data automatically on back navigation without additional "Done" button. Can I do some validation and save on back navigation (i.e. when user taps on standard back button) in a way that allows my to stop navigation and show some error message if something is wrong?

I see several other possibilities:

  • Create my custom left button and make it looks like standard back. (Why Apple didn't put this button style into public API?)
  • Add "Done" button and save data only if user taps it

but both these choices I like much less. So if there is a way to achieve what I want, I'd like to use it.

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

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

发布评论

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

评论(6

两个我 2024-08-22 12:37:35

基本上,您想要覆盖根视图控制器的 backBarButton 中的操作并在那里进行验证。如果验证通过,则调用 UINavigationController popViewControllerAnimated:,否则显示错误警报或其他内容。

但是,如果您尝试为根视图控制器 navigationItem.backBarButton 设置 targetaction 属性,它将不起作用。显然这些必须为零。

解决这个问题的方法可能是用自定义按钮替换标准后栏按钮。您可以使用标准 UIBarButtonItem 来做到这一点,但您会丢失“箭头”形状,因为它不能作为样式之一使用。解决方法可能是使用按钮的自定义视图。查看此帖子举个这样做的例子。

Basically you want to override the action from the backBarButton of your root view controller and do your validation there. If validation passes call the UINavigationController popViewControllerAnimated:, otherwise show an error alert or whatever.

However, if you try to set the target and action properties for the root view controllers navigationItem.backBarButton it won't work. Apparently these have to be nil.

A way round this maybe to replace the standard back bar button with a custom button. You could do that with a standard UIBarButtonItem, but you would lose the 'arrow' shape since this is not available as one of the styles. A workaround for that maybe to use a custom view for the button. Check out this thread for an example of doing that.

甜心 2024-08-22 12:37:35

只需在 UIViewController 子类中覆盖 [popViewControllerAnimated:] 即可。这样你就可以覆盖视图控制器离开屏幕的更一般的场景。

Just override [popViewControllerAnimated:] in your UIViewController subclass. That way you cover the more general scenario of the view controller leaving the screen.

宛菡 2024-08-22 12:37:35

我发现“检测何时按下后退按钮”的最佳方法是重新定义 viewWillDisappear ,如下所示:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (!isPushing) {
        // Apply your changes here
    }
}

布尔值 isPushing 将是您自己定义的一个,然后您设置仅在您自己推送另一个控制器的地方(如果您这样做...),它才为 True,这样您就可以区分 viewWillDisappear 被调用是因为您自己推送了一个新控制器,还是因为后退按钮被按下。

您通常会自己将另一个控制器推入表控制器中,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Example of pushing a new controller onto the navigation stack yourself...
    isPushing = YES;    // You have to set that boolean here...
    [self.navigationController pushViewController:myNewController animated:YES];
}

The best way I found to "detect when the Back button is pressed" is to redefine viewWillDisappear like so:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (!isPushing) {
        // Apply your changes here
    }
}

The boolean isPushing would be one that you define yourself, and you set it to True only in places where you push another another controller yourself (if you do...), that allows you to distinguish between viewWillDisappear being called because you're pushing a new controller yourself vs because the Back button was pressed.

You usually push another controller yourself in a table controller like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Example of pushing a new controller onto the navigation stack yourself...
    isPushing = YES;    // You have to set that boolean here...
    [self.navigationController pushViewController:myNewController animated:YES];
}
弱骨蛰伏 2024-08-22 12:37:35

另一种方法是,如果用户点击 UITableView 中的一行,则点击会启动视图控制器弹出。

Another approach, if, say, the user is tapping one row in a UITableView would be to have the tap initiate the view controller pop.

べ繥欢鉨o。 2024-08-22 12:37:35

另一种方法是让您的父视图完成这项工作。

假设您从数据视图(例如,联系信息)导航到字段编辑视图(例如,姓名编辑)。
在进入编辑视图之前,数据视图存储有关此事实的一些信息,例如:

self.navigatingTo = NavigatingToNameEdit;

然后在数据视图的 viewWillAppear 中检查并从编辑视图中提取相关信息:

if (self.navigatingTo == NavigatingToNameEdit) {
    self.name = self.nameEditView.name;
    // Don't forget to reset navigatingTo:
    self.navigatingTo = NavigatingToNone;
}

An alternative way is to have your parent view do the work.

Say you navigate from a data view (e.g., Contact Information) to a field-edit view (e.g., Name Edit).
Before going the edit view, the data view store some information about this fact, e.g.:

self.navigatingTo = NavigatingToNameEdit;

Then in your data view's viewWillAppear, check for that and extract the relevant information from the edit view:

if (self.navigatingTo == NavigatingToNameEdit) {
    self.name = self.nameEditView.name;
    // Don't forget to reset navigatingTo:
    self.navigatingTo = NavigatingToNone;
}
梦中楼上月下 2024-08-22 12:37:35

虽然您可能不久前已经摆脱了这个问题,但我今天才遇到这个问题。我的猜测是苹果不希望你覆盖这些后退按钮的操作。我的解决方案是显示和警报视图(显示在您刚刚移动到的视图控制器上)并使用 AlertViews 委托方法“clickedButtonAtIndex”将用户移回出现错误的屏幕。总的来说,我认为它实际上非常干净。我的2分钱...

While you've probably moved on from this a while ago, I just ran into this issue today. My guess is that Apple doesn't want you overriding the action of those back buttons. My solution was to display and alert view (which displays on the view controller that you've just moved to) and use AlertViews delegate method "clickedButtonAtIndex" to move the user back to the screen with the errors. Overall I think it actually comes across pretty clean. My 2 cents...

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