views:

54

answers:

3

Let's say you have two objects (for example, a UITextviews, but it could be any type) in your class. When the text view changes, you have a delegate method that catches the change.. but how can you tell programatically WHICH object was changed and called the delegate ??

(Possible thought) Basically how to you get the variable name of an object reference by a delegate ?

I have to be missing something, because this should be trivial, but I couldnt find anything. Note: In this case, its not possible to just break up the class to only have one object (there by bypassing ambiguity).. I looked for things like assigned variable names for nsobjects, nothing there

Here is the delegate method

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

}
A: 

usually the delegate method will receive a parameter in form of :(id) sender where sender is the actual component that sent the message and you can compare it to your fields sender == textField1

Reflog
thanks Reflog, unfortunately this delegate doesnt return an id.. but I might just be unclear on how to do the comparison.
textView delegate that you should does return an id `(UITextView*)textView` , this is your 'sender'. so now you can compare it to your outlets, like `if(textView1 == textView)`.
Reflog
A: 

Assuming that you have two UITextView properties named textView1 and textView2, which are set to your two text views (either through IB or in code), you would do this:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView == self.textView1) {
        // Handle textView1
    }
    if (textView == self.textView2) {
        // Handle textView2
    }
}
Nick Forge
A: 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

The bit I underlined. It's the text view that sent the delegate message.

JeremyP