views:

568

answers:

1

Editable TextView with Second NavBar - Text appears, but too late.

The app has a single Navigation Controller. I have an iPhone App that has basically three levels. Level 1 - Table with category Names Level 2 - Table with list of items for selected category Level 3 - Tabbed View with several views, including UITextView for details of item One to these Tabbed Views with a TextView is editable. When the user taps in the editable TextView the KeyBoard appears. User can type in the TextView. Character appear as they are typed.

At the top of this Level 3 TextView there is a NavBar(present for all 3 levels with changes) with a BackButton and a "home->Level1" button on the right.

All works just fine until in the editable TextView I add a second NavigationBar below the existing NavBar. This second NavBar has two buttons as well. They are Save/Cancel

When I click these Save and Cancel buttons the correct action methods are reach. All is perfect with one exception, The text which is typed does not appear in the TextView until either the Save or the Cancel button is touched. The relevent Button setup and action methods in my TabViewController.m are below.I need to persist this data.

I thought that getting a Notification from the TextView and the action handleTextChange would do the trick, but no luck. I am stuck. Please advise. A noobie badly stuck and out of ideas. Thanks for your time.

Mark

......... - (void)loadView {

self.myTextView = [[UITextView alloc] init];
self.myTextView.delegate = self;

self.view   = self.myTextView;
//UITextViewTextDidChangeNotification 
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleTextChange:) 
name:UITextViewTextDidChangeNotification
object:nil];
NSLog(@"Registered DG_HandleChangeTextNotification with notification center.");

}

  • (void)handleTextChange:(NSNotification * )note { [self.myTextView setNeedsDisplay] ; NSLog(@"...Handled Text Change."); }

  • (void)textViewDidBeginEditing:(UITextView *)textView { // provide my own Done/Save button to dismiss the keyboard

    saveNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; saveNavigationBar.barStyle = UIBarStyleBlackOpaque; UINavigationItem *doneItem = [[UINavigationItem alloc] init];
    doneItem.title = @"My Notes";

    UIBarButtonItem *doneItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAction:)]; UIBarButtonItem *cancelItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAction:)];

    [doneItem setRightBarButtonItem:doneItemButton animated:NO]; [doneItem setLeftBarButtonItem:cancelItemButton animated:NO]; [saveNavigationBar pushNavigationItem:doneItem animated:NO];

    [self.view addSubview:saveNavigationBar];

    [doneItem release]; [cancelItemButton release]; [doneItemButton release]; }

  • (void)saveAction:(id)sender { // finish typing text/dismiss the keyboard by removing it as the first responder

    self.text = self.myTextView.text; [self.saveNavigationBar removeFromSuperview];

    [self.myTextView resignFirstResponder];

}

  • (void)cancelAction:(id)sender { [self.saveNavigationBar removeFromSuperview];

    [self.myTextView resignFirstResponder];

}

A: 

The Second NavBar was hiding the area of the UITextEdit such that I had to type about four lines before I saw the text. I believe I need to lower the height of the UITextEdit by 44 pixels.

mbarron