简体   繁体   English

将两个方法链接到 UIBarButtonItem

[英]Link two methods to UIBarButtonItem

I have multiple text fields on the screen, and when I click "Done" in the navigation bar, I want to get the values in the text fields as well as go back to the previous screen.我在屏幕上有多个文本字段,当我在导航栏中单击“完成”时,我想获取文本字段中的值并返回上一个屏幕。

This is what I have so far that allows the user to go back to the previous screen:到目前为止,这是我允许用户返回上一个屏幕的内容:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDoctorButton:)];
    self.navigationItem.rightBarButtonItem = doneButton;

I wanted to try making two different methods:我想尝试制作两种不同的方法:

  1. getting the textfield values when the user presses down当用户按下时获取文本字段值

  2. popping the view when the user presses up当用户按下时弹出视图

I've seen something similar for normal UIButtons, but for some reason cannot figure out how to do it for a UIBarButtonItem.我在普通 UIButton 上看到过类似的东西,但由于某种原因无法弄清楚如何为 UIBarButtonItem 做到这一点。 Is this a feasible way to do this?这是一种可行的方法吗? If so, I'm not sure how to implement those two different methods for the same bar button.如果是这样,我不确定如何为同一个栏按钮实现这两种不同的方法。

Thanks!谢谢!

UIButtons are different from UIbarbuttonitems see this discussion: UIButtons 不同于 UIbarbuttonitems 看到这个讨论:

programmatically highlight UIBarButtonItem . 以编程方式突出显示 UIBarButtonItem

I am not sure if you can place a custom gesture recogniser on a BarbuttonItem, but if you can, that will be the way to go by adding 2 distinct recognisers我不确定您是否可以在 BarbuttonItem 上放置自定义手势识别器,但如果可以,这将是添加 2 个不同识别器的方法

To do that, you need an IBAction.为此,您需要一个 IBAction。 Go to your storyboard and ctrl+drag the button to the implementation file (you need to be in split view).转到您的故事板并按住 ctrl+将按钮拖动到实现文件(您需要处于拆分视图中)。 Select, IBAction and give it a name.选择 IBAction 并为其命名。 This is will be a selector to your button.这将是您按钮的选择器。 Then in that function/method, do whatever you'd like.然后在那个函数/方法中,做任何你想做的事。 You can get the textfield values from there as well as popping the view (look up how to do it).您可以从那里获取文本字段值以及弹出视图(查看如何操作)。 This can be done within this one IBAction function.这可以在这个 IBAction 函数中完成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM