2

I believe I can't disable it because I can't access that UIBarButttonItem programmatically
(with either viewWithTag or rightBarButtonItem).

Any suggestions (short from adding the interface without IB)?
As a test, I also tried adding a button programmatically (on the left of the nav bar), but it did not display in the nav bar.

RELEVANT CODE (In MyEditorViewControler.m):

    - (void)textFieldDidBeginEditing:(UITextField *)sender { //successfully executes when keyboard slides in

        UINavigationItem *item = self.navigationItem; //item  = 0x6420e0  OK.  (value at debugger breakpoints)
        UIBarButtonItem *doneButton4    = (UIBarButtonItem *) [self.view viewWithTag:44]; //doneButton4 = 0x0,  not OK.
        doneButton4.enabled = NO; 
    }
    - (void)textFieldDidEndEditing:(UITextField *)sender {  //successfully executes when keyboard  slides out.  
        ...
        UIButton* doneButton = (UIButton *)[self.view viewWithTag:44]; //Attempt to re-enable button.
        doneButton.enabled = YES;
    }
    - (void)viewDidLoad {   //Attempt to programmatically add a *left* button to the nav bar. Result: Button does not display in nav bar.
        .... 
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
        [leftBarButtonItem release];
    }

DETAILS
I would think this is a common case because that Done button:
a) is a UIBarButttonItem added from IB Library to navigation bar that is in a Scroll View that has some UITextField's.
b) behaves as expected (to save the user-entered data etc),
except for not getting disabled when keyboard appears.
c) IB > Inspector > Bar Button Item Attributes shows:
Identifier = Done
Tag = 44
Class = UIBarButtonItem

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
sambaMan
  • 45
  • 1
  • 6

2 Answers2

2

You should just be using

UIBarButtonItem *doneButton = self.navigationItem.leftBarButtonItem; 
doneButton.enabled = YES;

//Both of these should work, you shouldn't need any type of IBOutlets for this

UINavigationItem *item = self.navigationItem;
UIBarButtonItem *doneButton = item.leftBarButtonItem;
Daddy
  • 9,045
  • 7
  • 69
  • 98
  • Yes, I fixed that, but that is not the main issue. In code above, the question is either: 1) how get the test leftBarButtonItem to display first (and later enable/disable it) OR 2) how get the IB button (not classified as left or right) to disable (once that is done, is should be trivial to enable). – sambaMan Nov 09 '10 at 20:49
  • Yes, I have not added anything (Outlets or properties) to .h for this. – sambaMan Nov 09 '10 at 20:49
1

You can listen to a notification (UIKeyboardWillShowNotification) posted when the keyboard slides in:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

Then implement -keyboardWillShow:.

-(void)keyboardWillShow {
    UIButton *button = self.navigationItem.leftBarButtonItem;
    button.enabled = NO;
}

To reenable the button again, do the same for the UIKeyboardDidHideNotification

Björn Marschollek
  • 9,899
  • 9
  • 40
  • 66
  • With keyboardWillShow I have access to the leftBarButtonItem (great), but that button (created programmatically in viewDidLoad) does not display in the nav bar (created in IB). So I can't tell if it was disabled or not. How get that leftBarButtonItem to display? – sambaMan Nov 09 '10 at 21:00
  • SOLUTION: Many thanks for you responses. It works now. The trick was, instead of grabbing the button directly, to grab the nab bar first then grab the button from that. UINavigationBar *navBar = (UINavigationBar *)[self.view viewWithTag:22]; then enable and disable in the textFieldDidBeginEditing textFieldDidEndEditing. Many thanks. – sambaMan Nov 09 '10 at 22:58