带有第二个导航栏的可编辑 TextView - 文本出现,但为时已晚

发布于 2024-07-21 05:40:01 字数 2918 浏览 4 评论 0原文

带有第二个导航栏的可编辑 TextView - 文本出现,但为时已晚。

该应用程序有一个导航控制器。 我有一个 iPhone 应用程序,基本上分为三个级别。

  1. 级别 1 - 包含类别名称的表格

  2. 级别 2 - 包含所选类别的项目列表的表格

  3. 第 3 级 - 具有多个视图的选项卡式视图,包括用于显示项目详细信息的 UITextView 这些带有 TextView 的选项卡式视图之一是可编辑的。

    当用户在可编辑的 TextView 中点击键盘时 出现。 用户可以在 TextView 中输入内容。 人物出现 正如它们所输入的那样。

    在此 3 级 TextView 的顶部有一个导航栏(所有 3 级均存在) 更改),右侧有一个后退按钮和一个“home->Level1”按钮。

一切正常,直到在可编辑的 TextView 中添加第二个 NavigationBar 在现有导航栏下方。 第二个导航栏有两个按钮 以及。 它们是保存/取消。

当我单击这些“保存”和“取消”按钮时,正确的操作 方法都达到了。 一切都很完美,除了文字 键入的内容不会出现在 TextView 中,直到 触摸“保存”或“取消”按钮。 相关按钮设置和 我的 TabViewController.m 中的操作方法如下。 我需要坚持这一点 数据。

我认为从 TextView 获取通知和操作 handleTextChange 就可以解决问题,但运气不佳。 我被困住了。

.........
- (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];

}

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.

  1. Level 1 - Table with category Names

  2. Level 2 - Table with list of items for selected category

  3. 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. Characters 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 reached. 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 relevant 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.

.........
- (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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2024-07-28 05:40:01

第二个导航栏隐藏了 UITextEdit 的区域
因此我必须输入大约四行才能看到文本。 我相信
我需要将 UITextEdit 的高度降低 44 像素。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文