1

いくつかの NSTextField の変更をテストしようとしています。

私は使用しています:

- (void)controlTextDidEndEditing:(NSNotification *)notification {

    if ([notification object] == field1) 
        NSLog(@"field1: stringValue == %@", [field1 stringValue]);

    if ([notification object] == field2) 
        NSLog(@"field2: stringValue == %@", [field2 stringValue]);

    if ([notification object] == field3)
        NSLog(@"field3: stringValue == %@", [field3 stringValue]);

}

これはうまくいきますが、もっと良い方法があるのではないかと思います。ありがとう

4

2 に答える 2

1

これで十分です。

field1などなど、アウトレットになることを期待しています。

あなたは一歩良くすることができます:

- (void)controlTextDidEndEditing:(NSNotification *)notification {

    if ([notification object] == field1) 
        NSLog(@"field1: stringValue == %@", [field1 stringValue]);

    else if ([notification object] == field2) 
        NSLog(@"field2: stringValue == %@", [field2 stringValue]);

    else if ([notification object] == field3)
        NSLog(@"field3: stringValue == %@", [field3 stringValue]);

}
于 2013-06-02T10:25:55.180 に答える
1

Key-Value-Observing ( KVO) を使用して、値の変更をキャッチできます。

[field1 addObserver:self
         forKeyPath:@"text"
             options:(NSKeyValueObservingOptionNew |
                        NSKeyValueObservingOptionOld)
                context:NULL];

オブザーバーでメソッドを実装する必要があります。

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {

if ([keyPath isEqual:@"text"]) {
      if ( [object isMemberOfClass: [//class of object you have passed //]]){
         //project class you are processing and then use  a log
         NSLog(@"object: stringValue == %@", [field2 stringValue]);
      }

}
/*
 Be sure to call the superclass's implementation *if it implements it*.
 NSObject does not implement the method.
 */
[super observeValueForKeyPath:keyPath
                     ofObject:object
                       change:change
                       context:context];

}

于 2013-06-02T11:23:24.083 に答える