简体   繁体   English

当按下导航后退按钮时,将值返回到呈现视图控制器

[英]Return values to presenting view controller when navigation back button pressed

I'm having trouble piecing this all together. 我在将所有这些拼凑在一起时遇到麻烦。 I have a view controller that opens up another (pushes it on to the navigation stack). 我有一个视图控制器,可以打开另一个(将其推送到导航堆栈上)。 On that presented view controller, the user enters a value in a text view. 在该显示的视图控制器上,用户在文本视图中输入一个值。 When the user pushes the back button in the navigation, I want to be able to pass the value that they entered in the text view back to the presenting controller. 当用户在导航中按下后退按钮时,我希望能够将他们在文本视图中输入的值传递回呈现控制器。

I've looked for a way to use unwind segue with the back button but haven't found anything. 我一直在寻找一种使用后退按钮进行放松搜索的方法,但没有发现任何东西。 When I create my back button (programmatically) I use initWithTitle:style:target:action but I'm not sure how in implementing the action method that I'll be able to access the value set in the presented controller. 当我(以编程方式)创建后退按钮时,我使用initWithTitle:style:target:action,但是我不确定在实现action方法时如何能够访问所提供的控制器中设置的值。 Might have to use a delegate to link the two, but not sure of the exact integration point for this scenario. 可能必须使用委托来链接两者,但不能确定此方案的确切集成点。

I feel like I'm so close here and a little help would get me there. 我觉得我离这里很近,只要有一点帮助,我就可以到达那里。 Thanks! 谢谢!

The two most common models to use for this interaction are for the child view controller to have either a delegate or a completion block. 用于此交互的两个最常见的模型是子视图控制器具有委托或完成块。 Either would be set in the prepareForSegue method. 可以在prepareForSegue方法中进行设置。 My personal preference is the completion block method just because it keeps code contained, but ymmv. 我个人比较喜欢使用完成块方法,因为它保留了代码,但包含ymmv。

There are also multiple models for detecting when your child view controller is dismissed and you need to invoke the delegate and/or completion: 还有多种模型可以检测何时关闭子视图控制器,而您需要调用委托和/或完成:

  1. Use a custom back button. 使用自定义后退按钮。 Not a fan of this as it can be an issue to create a back button that really looks and acts like the Apple original, especially if supporting iOS 6 and iOS 7. 不喜欢这样做,因为创建确实看起来和行为类似于Apple原始设备的后退按钮可能是一个问题,尤其是在支持iOS 6和iOS 7的情况下。

  2. Hook viewDidDisappear and see if you're still in the navigation controller's viewControllers array. 钩住viewDidDisappear,看看您是否仍在导航控制器的viewControllers数组中。 This is better as the back button works right, but it still feels kind of hokey. 后退按钮可以正常工作,这样更好,但仍然感觉有些曲折。

  3. Use the UINavigationBarDelegate method navigationBar:shouldPopItem: This is attractive, especially if you have other validation that needs to happen like checking for saved/unsaved values. 使用UINavigationBarDelegate方法navigationBar:shouldPopItem:这很有吸引力,尤其是在您需要进行其他验证(例如检查保存/未保存的值)的情况下。 To implement this you'll have to subclass UINavigationController and forward the method to your child view controller. 要实现这一点,您必须继承UINavigationController的子类,并将该方法转发给子视图控制器。

EDIT: Details on Option 2: 编辑:关于选项2的详细信息:

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if(![self.navigationController.viewControllers containsObject:self])
    {
        // We're not still in the navigation stack so we must've been
        //  popped.  If we were pushed, viewDidDisappear would be called
        //  but viewControllers containsObject:self would be true
    }
}

EDIT: Clarified Option 3: in your navigation controller subclass 编辑:澄清了选项3:在导航控制器子类中

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    UIViewController*   top = self.topViewController;
    if([top respondsToSelector:@selector(navigationBar:shouldPopItem:)])
        return [(id)top navigationBar:navigationBar shouldPopItem:item];
    return [super navigationBar:navigationBar shouldPopItem:item];
}

Then you can implement navigationBar:shouldPopItem: in the classes that need the functionality. 然后,您可以在需要该功能的类中实现navigationBar:shouldPopItem:

the back button does not actually comes up with any event associated with itself so that you can pass the values between the previous and to be Popped ViewController. 后退按钮实际上并不带有与其关联的任何事件,因此您可以在上一个和要弹出的ViewController之间传递值。

You would have to implement Delegate pattern to pass values. 您将必须实现Delegate模式以传递值。 In this case as you cant catch when backButton is pressed, you need to use custom leftBarButtonItem or use a image with < in itself. 在这种情况下,当您按下backButton时无法捕获时,您需要使用自定义的leftBarButtonItem或使用本身带有<的图像。

暂无
暂无

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

相关问题 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 迅速按下导航栏后退按钮时重新加载视图控制器2 - Reloading view controller when navigation bar back button is pressed in swift 2 呈现视图控制器时设置后退按钮 - Setting back button when presenting a view controller 按下导航控制器后退按钮时如何导航到其他View Controller? - How to navigate to some other View Controller when Navigation Controller back button is pressed? 当导航栏的内置后退按钮被按下时弹出到根视图控制器 - Pop to the root view controller when the navigation bar's built in back button is pressed Xcode:检测何时按下导航控制器实现的“后退按钮” - Xcode: Detecting when a navigation controller implemented “back button” is pressed 按下导航后退按钮时,SWRevealViewController关闭左侧视图 - SWRevealViewController close left side view when navigation back button pressed 如何检查视图控制器是否在导航控制器的其他页面按下“后退”按钮后出现? - how to check if a view controller appeared after some other page in navigation controller pressed back button? 通过UIPresentationController子类呈现视图控制器时导航栏跳转 - Navigation Bar jumping when presenting view controller via UIPresentationController subclass 显示视图控制器时缺少导航和标签栏 - Navigation and tab bar missing when presenting view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM