0

私のプロジェクトには、検索バーがあります。

検索バーのテキストが変更された場合、私は知る必要があります

したがって、次のように比較する2つの静的 NSMutableString を宣言します

static NSMutableString *latestKeyWord;
static NSMutableString *secondaryKeyWord;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [testViewController setSecondaryKeyWord:SBar.text];
    [testViewController setLatestKeyWord:SBar.text];
    [testViewController changeKeyWord];
}
    +(void)setSecondaryKeyWord:(NSMutableString*) text
{
    if (!secondaryKeyWord) {
        secondaryKeyWord = [NSMutableString stringWithCapacity:200];
    }
    [secondaryKeyWord setString:text];
}

+(void)setLatestKeyWord:(NSMutableString*)text
{
    if (!latestKeyWord) {
        latestKeyWord = [NSMutableString stringWithCapacity:200];
    }
    [latestKeyWord setString:secondaryKeyWord];
}

-(BOOL)changeKeyWord
{
    if ( ! [latestKeyWord isEqualToString:secondaryKeyWord]) {
         NSLog(@"changed");
        return TRUE;
    }else {
        NSLog(@"not change");
        return FALSE;
    }
}

在庫があるだけで、理由がわかりません。

4

1 に答える 1

0

テキスト フィールド内のテキストが変更されたときに通知を受け取りたい場合は、ターゲット/アクションをテキスト フィールドに追加します。

@interface ViewController : UIViewController
@property (nonatomic) UITextField *field;
@property (nonatomic) NSString *searchText;
@end


@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect frame = CGRectMake(50, 50, 200, 25);
    self.field = [[UITextField alloc] initWithFrame:frame];
    [self.field addTarget:self action:@selector(textChanged:) 
         forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.field];
}

- (void)textChanged:(id)sender {
    self.searchText = self.field.text;
    NSLog(@"Text changed to: %@", self.searchText);
    // restart searching here.
}
@end
于 2012-10-07T12:58:59.573 に答える