我需要取消选择器。我能 100% 保证我取消了选择器吗?

发布于 2024-12-19 16:45:45 字数 980 浏览 1 评论 0原文

我想更改 UISearchBar 中的文本,但不想调用 PerformSearch。我可以通过两种方式取消选择器。

第一种方法:我使用方法 func1。

@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
-(void) func1
{
  self.searchBar.delegate = self;
  self.searchBar.text = @"";
  [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(performSearch:) object:nil];
}

- (void)searchBar:(UISearchBar *)searchBar_ textDidChange:(NSString *)searchText
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(performSearch:) object:nil];
    [self performSelector:@selector(performSearch:) withObject:nil afterDelay:1];
} 

第二种方式:我正在使用 func2

-(void) func2
{
  self.searchBar.delegate = nil;
  self.searchBar.text = @"";
  self.searchBar.delegate = self;
}

我是否能 100% 保证我取消了 func1 中的选择器? func2 更好吗?

更新:我正在寻找通用解决方案,而不仅仅是 self.searchBar.text = @"";self.searchBar.text = @"Restaurants";

I want to change text in UISearchBar, but I don't want to call performSearch. I could cancelled the selector in two ways.

First way: I'm using method func1.

@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
-(void) func1
{
  self.searchBar.delegate = self;
  self.searchBar.text = @"";
  [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(performSearch:) object:nil];
}

- (void)searchBar:(UISearchBar *)searchBar_ textDidChange:(NSString *)searchText
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(performSearch:) object:nil];
    [self performSelector:@selector(performSearch:) withObject:nil afterDelay:1];
} 

Second way: I'm using func2

-(void) func2
{
  self.searchBar.delegate = nil;
  self.searchBar.text = @"";
  self.searchBar.delegate = self;
}

Do I have a 100% guarantee that I cancelled the selector in the func1? Is func2 better?

UPDATE: I'm looking for universal solutions, not just self.searchBar.text = @""; or self.searchBar.text = @"Restaurants";.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动次打次papapa 2024-12-26 16:45:45

第一种方法非常复杂,您正在调用方法并尝试取消它们。如果取消没有在正确的时间到达,该方法将被调用。所以不要这样做,如果应用程序增长并且情况变得更加复杂,这将是一场噩梦。

第二种解决方案更加健壮,您正在删除委托。因此可以保证委托不会被调用,因为它不存在。但 iOS 也无法调用委托来处理您可能想要处理的其他事情。除此之外;您应该在设备上对此进行测试,因为我认为当没有委托返回任何结果时,iOS 将删除所有结果。但这取决于您如何实现搜索的其他部分。

我的建议:从 UISearchDisplayDelegate 协议实现 searchDisplayController:shouldReloadTableForSearchString: 方法。通过此方法,您可以控制搜索栏是否应重新加载包含搜索结果的表格。在您更改搜索字符串之前,您自己设置一个标志,因此此方法现在可以确定何时返回内容。

示例伪代码:

BOOL ignoreChanges = NO;

-(void)editSearchString
{
    ignoreChanges = YES;
    self.searchBar.text = @"some_string";
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{
    BOOL shouldReload = !ignoreChanges;
    ignoreChanges = NO;

    return shouldReload;
}

这样您就可以忽略代码对搜索字符串所做的更改,但您将尊重用户所做的更改。当然可以将检查放在另一个函数中的 ignoreChanges 布尔值上,例如 searchBar:textDidChange: 方法,如果这比我的建议更适合您的需求!

祝你好运!

First method is quite complicated, you're calling methods and try to cancel them. If the cancel doesn't arrive at the right time the method will be called. So don't do this it's a nightmare if the App grows and situations get more complex.

The second solution is somewhat more robust, you are removing the delegate. So it's guaranteed that the delegate won't get called, because it isn't there. But iOS is also not able to call the delegate for other things that you maybe want to handle. Besides that; You should test this on a device, because I think iOS will remove all results when there is no delegate that returns any results. But this will depend on how you've implemented other parts of the search.

My advice: Implement the searchDisplayController:shouldReloadTableForSearchString: method from the UISearchDisplayDelegate protocol. From this method you can control if the searchbar should reload the table with search results. Right before you alter the search string yourself set a flag, so this method nows when to return what.

Example pseudocode:

BOOL ignoreChanges = NO;

-(void)editSearchString
{
    ignoreChanges = YES;
    self.searchBar.text = @"some_string";
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{
    BOOL shouldReload = !ignoreChanges;
    ignoreChanges = NO;

    return shouldReload;
}

This way you ignore the change of the search string that is done by your code, but you will respect changes made by the user. It is of course possible to place the check on the ignoreChanges boolean in another function, like the searchBar:textDidChange: method if that suits your needs better then my suggestion!

Good luck!

被你宠の有点坏 2024-12-26 16:45:45

查找 将 NSOperation 子类化为并发且可取消

尝试使用 >BJ Homer 的回答很好地解释了如何解决它

希望对您有所帮助!

Try to look up Subclassing NSOperation to be concurrent and cancellable

with BJ Homer answer that explain well how to solve it

hope it helps you!

一直在等你来 2024-12-26 16:45:45

回答你提出的问题:是的。我从来没有遇到过 cancelPreviousPerformRequestsWithTarget: 没有做它应该做的事情的问题。

但我倾向于使用ignoreChanges方法,而不是发布和取消消息。

To answer the question you phrased: yes. I've never had trouble with cancelPreviousPerformRequestsWithTarget: not doing what it's supposed to do.

But I'd lean towards the ignoreChanges approach rather than posting and cancelling messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文