0

I've got two textviews on my view and I want them both to be editable.

But each one pertains to a different record in my database.

How would I be able to detect which textView is being edited?

Here is my code so far

  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()
    }
}

What I'm currently doing is detecting if the view is right aligned or center aligned to be able to tell them apart. This works but it's not ideal as I'd like to have both of them center aligned. But I'm unaware which field in the textView object would contain an ID or some method of identification as to which textView the function was called for.

4
  • 1
    Do you have properties (IBOutlets) for each of the text views? If so, just compare the text view that was provided to the delegate method with the properties to see which one it is
    – Paulw11
    Jul 28, 2016 at 2:06
  • Yes I've created IBOutlets for each textview but what do you mean by compare the textview properties? Such as if (textView == t_view1) ? Assuming t_view1 is the IBOutlet variable to a specific textview.
    – Basic
    Jul 28, 2016 at 2:10
  • 1
    Also you can assign different tags to textView and check it in your delegate as textView.tag. Jul 28, 2016 at 2:13
  • You can simply check by textView == textViewOne if you made textViewOne as iboutlet isnt it....Its only hard if you dont have reference to it
    – Tj3n
    Jul 28, 2016 at 3:09

1 Answer 1

7

As long as you have properties that refer to the two text views you can simply see which one was passed to your delegate and act accordingly:

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()
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.