0

在我的项目中,我有一个搜索栏。

如果搜索栏中的文字发生了变化,我需要知道

所以我声明了两个静态 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 回答