gpt4 book ai didi

ios - 将字符串预加载到搜索栏并从 segue 搜索

转载 作者:行者123 更新时间:2023-11-29 00:50:20 25 4
gpt4 key购买 nike

问题类似于这个问题:Setting searchBar.text in view did load

我的关键字从 V1 顺利传递到 V2;我正在尝试将其设置为搜索栏文本并从之前的 segue 进行搜索。

字符串称为 searchText

我试过了

    self.searchBar.text = self.searchText;

在 V2 的 viewWillAppear 和 viewDidLoad 中,但我的搜索栏中没有显示任何内容。

从 V1 到 V2 后,如何将我的 searchText 放入搜索栏并过滤我的表格?

最佳答案

在将其设置为搜索栏文本之前,您确定该字符串不为空吗?

以下代码非常适合我。

@interface ViewController (){
NSString *searchText;
}

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
searchText = @"I love ice cream!";
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.searchBar.text = searchText;
}

这是一种检查字符串是否为空的方法:

self.searchBar.text = (searchText == nil || searchText == (id)[NSNull null] || searchText.length == 0) ? @"To bad, an empty string" : searchText;

关于ios - 将字符串预加载到搜索栏并从 segue 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com