[英]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: 还有多种模型可以检测何时关闭子视图控制器,而您需要调用委托和/或完成:
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的情况下。
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.
后退按钮可以正常工作,这样更好,但仍然感觉有些曲折。
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.