iphone - UISearchBar detect when user stop type and don't search immediately so detect pause -
i'm searching implementing uisearchbar retrieve information url, , default method:
- (void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext{
i can detect when text change , perform fetch of url, in way, text type slow because iphone searching url, want start fetching of url when user stop writing second, want detect pause of typing refresh table view retrieving information url. have found older post of request , have tried solution me don't work:
- (void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext { [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(request:) object:searchtext]; //..... [self performselector:@selector(request:) withobject:searchtext afterdelay:1.5]; } -(void)request:(nsstring *)mystring { nslog(@"%@",mystring); }
in way when i'm typing request method not called when stop typing it's called every character type, same of default method, wrong something? or implementation isn't correct?
it looks provided solution isn't working because "searchtext" argument cancelpreviousperformrequestswithtarget:selector:object:
doesn't match "searchtext" argument previous call performselector:withobject:afterdelay:
; delayed searches queued every text change, , never cancelled.
you use nstimer
delay invocation of search, , cancel timer whenever text changes: give object nstimer
property or field; in searchbar:textdidchange:
cancel existing timer, create , schedule new one. target of timer should call search method.
something (off top of head):
// in class' .h object fields { ... nstimer *searchdelayer; // weak ref, adding "[searchdelayer invalidate], searchdelayer=nil;" dealloc wouldn't hurt ... } // in .m -(void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext { [searchdelayer invalidate], searchdelayer=nil; if (yes /* ...or whatever validity test want apply */) searchdelayer = [nstimer scheduledtimerwithtimeinterval:1.5 target:self selector:@selector(dodelayedsearch:) userinfo:searchtext repeats:no]; } -(void)dodelayedsearch:(nstimer *)t { assert(t == searchdelayer); [self request:searchdelayer.userinfo]; searchdelayer = nil; // important because timer release , dealloc }
some programmers might less cavalier weak references i'm being here.
[edited add:]
here's how might if set on using cancelpreviousperformrequestswithtarget...:
// in class' .h object fields { ... nsstring *priorsearchtext; // not weak ref, adding "[priorsearchtext release]" dealloc required ... } // in .m -(void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext { [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(request:) object:priorsearchtext]; [priorsearchtext release], priorsearchtext = [searchtext retain]; if (yes /* ...or whatever validity test want apply */) [self performselector:@selector(request:) withobject:searchtext afterdelay:1.5]; }
i don't think i've ever used cancelpreviousperformrequestswithtarget:... real, don't know if hides surprises. if have trouble, add nslogs, delayed searches not getting cancelled when should.
Comments
Post a Comment