简体   繁体   中英

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. 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:

programmatically highlight 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

To do that, you need an IBAction. Go to your storyboard and ctrl+drag the button to the implementation file (you need to be in split view). Select, IBAction and give it a name. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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