如何快速检测正在编辑的文本视图

时间:2016-07-28 01:57:54

标签: ios swift textview

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

但每个都属于我数据库中的不同记录。

我如何能够检测正在编辑哪个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或一些识别函数的文本视图方法。

1 个答案:

答案 0 :(得分:5)

只要您拥有引用两个文本视图的属性,您就可以看到哪个属性传递给您的代理并采取相应的行动:

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()
    }
}
相关问题