Home > DeveloperSection > Forums > How to Toggle UIBarButtonItem on KeyboardDidShow
Goti Bandu

Total Post:119

Points:835
Posted on    October-13-2014 12:57 AM

 iPhone iPhone  Mobile Development 
Ratings:


 2 Reply(s)
 832  View(s)
Rate this:

Purpose: I am attempting to toggle the "Save" UIBarButtonItem with the "Hide Keyboard" UIBarButtonItem whenever the Keyboard appears (and then do the opposite when the "Hide Keyboard" button is clicked).

So far I have created two UIBarButtonItems and connected them both to Interface Builder.

@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *hideKeyButton;

This is the code I have setup so far in my main:

- (void)keyboardDidShow:(NSNotification *)aNotification {

            // Show HideKey Button

            // Hide Save Button

        }

 

        - (void)keyboardWillHide:(NSNotification *)aNotification {

            // Show Save Button

            // Hide HideKey Button

        }

On Interface Builder, the Save button is present by default. Programatically, how do I show the HideKey button and Hide the Save button? Thanks.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Supported
Posted on    October-13-2014 12:59 AM

There are two parts to make this work.

First, you need to register for the notifications, what you can do is, in your viewDidLoad, add the following:

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardDidShow:)

                                             name:UIKeyboardDidShowNotification

                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWillHide:)

                                             name:UIKeyboardWillHideNotification

                                           object:nil];

Secondly, in your event handlers, simply set the buttons:

            - (void)keyboardDidShow:(NSNotification *)aNotification {

    self.navigationItem.rightBarButtonItem = hideKeyButton;

}

 

- (void)keyboardWillHide:(NSNotification *)aNotification {

    self.navigationItem.rightBarButtonItem = saveButton;

}


Pawan Shukla

Total Post:35

Points:253
Posted on    October-13-2014 1:01 AM

Instead of using IB for this task, I would do it programmatically.

Something like this:

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave

                                                                             target:self

                                                                             action:@selector(saveButtonPressed:)];

[self.navigationItem setRightBarButtonItem:saveButton];

And in a similar fashion to create and set the HideKey button.


Then, of course you may want to cache the UIBarButtonItems, setting lazy @properties for them.


Don't want to miss updates? Please click the below button!

Follow MindStick