开发者

how to tell which object called a delegate method (objective c)

Let's say you have two objects (for example, a 开发者_如何学CUITextviews, 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 {

}


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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜