Editable TextView with second NavBar - text appears but too late
Editable TextView with second NavBar - text appears but too late.
The app has one navigation controller. I have an iPhone application that has basically three levels.
-
Level 1 - Table with category names
-
Level 2 - Table with a list of items for the selected category
-
Level 3 - Tab with multiple views, including a UITextView for detailed item information. One of these tabs is editable using a TextView.
When the user inserts an editable TextView the KeyBoard appears. The user can enter a TextView. The characters appear as they are printed.
At the top of this level 3 TextView there is a NavBar (present for all 3 levels with change) with a BackButton and "home-> Level1" on the right.
Everything works fine until in the edited TextView I add a second NavigationBar below the existing NavBar. This second NavBar has two buttons as well. It's Save / Cancel.
When I click the Save and Cancel buttons, the correct action methods are achieved. Everything is fine, with one exception, the text that is being typed is not displayed in the TextView until the "Save" or "Cancel" button. The corresponding button setup and action methods in mine TabViewController.m
are given below. I need to persist in this data.
I thought getting Notification from TextView and action handleTextChange would do the trick, but no luck. I am stuck.
.........
- (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 source to share