【问题标题】:how to detect which textview is being edited swift如何快速检测正在编辑哪个文本视图
【发布时间】:2016-07-28 01:57:53
【问题描述】:

我的视图上有两个文本视图,我希望它们都是可编辑的。

但每一个都与我数据库中的不同记录有关。

如何检测正在编辑的 textView?

这是我目前的代码

  func textViewDidChange(textView: UITextView) { //Handle the text changes here
    if(textView.textAlignment == .Center){
        PFUser.currentUser()!["bio"] = textView.text
        PFUser.currentUser()!.saveInBackground()
    }
    else{
        PFUser.currentUser()!["displayName"] = textView.text
        PFUser.currentUser()!.saveInBackground()
    }
}

我目前正在做的是检测视图是右对齐还是居中对齐,以便能够区分它们。 这可行,但并不理想,因为我想让它们都居中对齐。但我不知道 textView 对象中的哪个字段将包含一个 ID 或某种识别方法,以识别调用该函数的 textView。

【问题讨论】:

  • 每个文本视图都有属性(IBOutlets)吗?如果是这样,只需将提供给委托方法的文本视图与属性进行比较,看看它是哪一个
  • 是的,我已经为每个 textview 创建了 IBOutlets,但是比较 textview 属性是什么意思?比如 if (textView == t_view1) ?假设 t_view1 是特定文本视图的 IBOutlet 变量。
  • 您也可以将不同的tags 分配给textView 并在您的delegate 中检查为textView.tag
  • 如果您将textViewOne 设为iboutlet,您可以简单地通过textView == textViewOne 进行检查......如果您没有参考它,这只是困难

标签: ios swift textview


【解决方案1】:

只要您有引用两个文本视图的属性,您就可以简单地看到哪个被传递给您的委托并采取相应的行动:

func textViewDidChange(textView: UITextView) { //Handle the text changes here

    guard let currentUser = PFUser.currentUser() else {
        return
    }
    if (textView == self.bioTextView){
        currentUser["bio"] = textView.text
        currentUser.saveInBackground()
    } else {
        currentUser["displayName"] = textView.text
        currentUser.saveInBackground()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多