---
title: "How to Toggle UIBarButtonItem on KeyboardDidShow"  
description: "How to Toggle UIBarButtonItem on KeyboardDidShow"  
author: "Anonymous User"  
published: 2014-10-13  
updated: 2014-10-13  
canonical: https://www.mindstick.com/forum/2370/how-to-toggle-uibarbuttonitem-on-keyboarddidshow  
category: "iphone"  
tags: ["iphone", "mobile development"]  
reading_time: 2 minutes  

---

# How to Toggle UIBarButtonItem on KeyboardDidShow

[Purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose): I am attempting to toggle the "Save" UIBarButtonItem with the "[Hide Keyboard](https://www.mindstick.com/forum/12987/how-to-hide-keyboard-on-outside-tap-for-edittext-in-android)" UIBarButtonItem whenever the Keyboard appears (and then do the opposite when the "Hide Keyboard" [button is clicked](https://www.mindstick.com/forum/157821/write-a-jquery-code-that-selects-all-the-checkboxes-in-a-form-when-a-select-all-button-is-clicked)).

So far I have created two UIBarButtonItems and [connected](https://www.mindstick.com/articles/12941/link-building-and-search-engine-rankings-how-are-they-connected) them both to [Interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) [Builder](https://www.mindstick.com/articles/12472/mgs-front-end-builder-for-magento-2-best-selling-product-on-magento-marketplace).

@[property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) (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](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) by default. Programmatically, how do I show the HideKey button and Hide the Save button? Thanks.\

## Replies

### Reply by Anonymous User

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](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net).

Then, of course you may want to cache the UIBarButtonItems, setting lazy @properties for them.

### Reply by Anonymous User

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;

}


---

Original Source: https://www.mindstick.com/forum/2370/how-to-toggle-uibarbuttonitem-on-keyboarddidshow

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
