【问题标题】:How to update UILabel that is a subview of inputView?如何更新作为 inputView 子视图的 UILabel?
【发布时间】:2011-06-04 15:36:30
【问题描述】:

我有 UITextView,用户可以在其中输入文本。显示键盘时,我在其上添加带有 UIlabel 的 inputView。我希望这个 UIlabel 保存文本的字符长度。这似乎很容易,但不幸的是,当用户更改文本时它不会更新这个单词计数器 UILabel..

这就是我加载 inputView 的方式

_textView.inputView = [self inputAccessoryView];

在 inputAccessoryView 中,我只是将 UILabel 添加为子视图。显示键盘时,UILabel 也与 inputView 一起显示。我在

上跟踪更改
- (void)textViewDidChange:(UITextView *)textView

不幸的是,UILabel 从未更新(重绘)。当我登录控制台它的值时,该值是正确的,因此它正在更新,但 UIlabel 永远不会重绘并保持默认值。

谁能帮我解决这个问题?

【问题讨论】:

  • 您是如何尝试更新标签的?即使 UI 没有显示该值,您如何确定该值已更新?

标签: iphone objective-c uitextviewdelegate inputview


【解决方案1】:

你有没有

_textView.delegate = self;

?

【讨论】:

    【解决方案2】:

    我知道那是 5 年前的事了,但它可能会帮助像我一样偶然发现你的问题的其他人。

    我使用 UIToolbar 作为我的 inputAccessoryView(在我的例子中,它有一个标签、一个灵活的分隔符和一个按钮)。 在 textFieldEditingChanged 事件中,我像这样重建工具栏的一部分

    @IBAction func textFieldEditingChanged(_ sender: UITextField) {
    
        //get a reference to the toolbar
        let toolBar = sender.inputAccessoryView as! UIToolbar
    
        //create a new label and set size + other properties
        toolbarLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 22))
        toolbarLabel.text = mySpecificCalculatedString(sender.text!)
        toolbarLabel.font = defaultFont(17)
        toolbarLabel.adjustsFontSizeToFitWidth = true
        let width = toolbarLabel.textRect(forBounds: CGRect(x: 0, y: 0, width: maxWidthForLabel, height: 0), limitedToNumberOfLines: 1).size.width
        toolbarLabel.frame = CGRect(x: 0, y: 0, width: width, height: 22)
        toolbarLabel.textColor = .black
        toolbarLabel.tag = 666
    
        //rebuild the specific tolbar item
        let customView = UIBarButtonItem(customView: toolbarLabel)
        toolBar.items![0] = customView
    }
    

    注意:简单地更改标签的文本对我也不起作用,我必须重新初始化它。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多