Reputation: 4062
I have two NSTextfield, and I want to use the same method for each one:
-(void)controlTextDidChange: (id)sender {
[label setStringValue:[textfield stringValue]];
}
I would like to use a different label/textfield
couple depending on which NSTextField send the message. Is this information available in the sender
object, or do I have to create a new delegate ?
Upvotes: 1
Views: 228
Reputation: 4062
Here is how I solved it:
-(void)controlTextDidChange: (id)sender {
[[[window contentView]
viewWithTag:( [[sender object] tag] + 100 )]
setStringValue:[ [sender object] stringValue]];
I set in interface builder the tag of the labels to a value of: 100 + the tag of the associated NSTextField. There is also no need for IBOutlet.
Upvotes: 1
Reputation: 727137
The [sender object]
is your textfield
, so getting the stringValue
is easy. Getting the associated label, however, is not: you need to build your own scheme to find it.
One way could be by marking your text fields by setting their tag
properties to different numbers. If you set the tag of text field for the first label to 1 and the tag of text field for the second label to 2, you can do something like this:
-(void)controlTextDidChange: (id)sender {
NSTextField *textfield = [sender object];
NSLabel *label = nil;
switch ([textfield tag]) {
case 1: label = myLabelOne; break;
case 2: label = myLabelTwo; break;
}
[label setStringValue:[textfield stringValue]];
}
Upvotes: 2